在 UIImage 上添加一层颜色
Adding a layer of color over a UIImage
我希望在被点击时短暂地制作我的 UIImage "highlight"。还不确定颜色,但为了争论起见,让我们说蓝色。因此,您点击图片,它会短暂显示蓝色,然后将您导航到详细信息页面,以便在另一个屏幕上编辑内容。
从一些初步阅读来看,正确的做法似乎是使用 Quartz 框架并执行以下操作:
imageView.layer.backgroundColor = UIColor.blueColor()
imageView.layer.opacity = 0.7
我想这个想法是你改变图像后面图层的背景,然后通过设置图像的不透明度,蓝色 "bleeds through" 一点点,给你一个略带蓝色的图像?
然而,当我尝试上述操作时,图像本身周围出现了蓝色边框,并且根据不透明度,蓝色要么是深色要么是浅色。实际图像不再变蓝,但它 确实 对不透明度做出反应(这意味着如果我将其设置为类似 .1 的值,图像会非常褪色并且几乎看不见)。为什么图像反应正确,但不显示蓝色?
非常感谢!
据我所知,改变不透明度会改变整个视图的不透明度,这意味着不仅仅是 UIImageView 持有的 UIImage。因此,不是褪色以显示 UIImageView 的背景颜色,而是整个视图的不透明度只是降低,如您所见。
另一种方法是在 UIImageView 之上添加一个初始透明的 UIView 并改为更改其不透明度:
UIView *blueCover = [[UIView alloc] initWithFrame: myImageView.frame];
blueCover.backgroundColor = [UIColor blueColor];
blueCover.layer.opacity = 0.0f;
[self.view addSubview: blueCover];
[UIView animateWithDuration:0.2f animations^{
blueCover.layer.opacity = 0.5f
}];
您可以尝试更改色调:
UIImage *image = [UIImage imageNamed:@"yourImageAsset"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.tintColor = [UIColor blueColor];
imageView.image = image;
这个点按突出显示可能是您可以使用 UIButton 做的事情吗? UIButton 具有所有这些状态,并且可能更容易使用,特别是在您点击它之后确实需要发生某些事情的情况下。最坏的情况是你有一个 UIButton 在点击时不会触发任何方法。
以下是我在 IOS 9 中使用色调和色调不透明度的方式 Swift -
//apply a color to an image
//ref -
//ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming
func getTintedImage() -> UIImageView {
var image : UIImage
var imageView : UIImageView
image = UIImage(named: "someAsset")!
let size : CGSize = image.size
let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height)
let redCover : UIView = UIView(frame: frame)
redCover.backgroundColor = UIColor.redColor()
redCover.layer.opacity = 0.75
imageView = UIImageView()
imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic)
imageView.addSubview(redCover)
return imageView
}
我希望在被点击时短暂地制作我的 UIImage "highlight"。还不确定颜色,但为了争论起见,让我们说蓝色。因此,您点击图片,它会短暂显示蓝色,然后将您导航到详细信息页面,以便在另一个屏幕上编辑内容。
从一些初步阅读来看,正确的做法似乎是使用 Quartz 框架并执行以下操作:
imageView.layer.backgroundColor = UIColor.blueColor()
imageView.layer.opacity = 0.7
我想这个想法是你改变图像后面图层的背景,然后通过设置图像的不透明度,蓝色 "bleeds through" 一点点,给你一个略带蓝色的图像?
然而,当我尝试上述操作时,图像本身周围出现了蓝色边框,并且根据不透明度,蓝色要么是深色要么是浅色。实际图像不再变蓝,但它 确实 对不透明度做出反应(这意味着如果我将其设置为类似 .1 的值,图像会非常褪色并且几乎看不见)。为什么图像反应正确,但不显示蓝色?
非常感谢!
据我所知,改变不透明度会改变整个视图的不透明度,这意味着不仅仅是 UIImageView 持有的 UIImage。因此,不是褪色以显示 UIImageView 的背景颜色,而是整个视图的不透明度只是降低,如您所见。
另一种方法是在 UIImageView 之上添加一个初始透明的 UIView 并改为更改其不透明度:
UIView *blueCover = [[UIView alloc] initWithFrame: myImageView.frame];
blueCover.backgroundColor = [UIColor blueColor];
blueCover.layer.opacity = 0.0f;
[self.view addSubview: blueCover];
[UIView animateWithDuration:0.2f animations^{
blueCover.layer.opacity = 0.5f
}];
您可以尝试更改色调:
UIImage *image = [UIImage imageNamed:@"yourImageAsset"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.tintColor = [UIColor blueColor];
imageView.image = image;
这个点按突出显示可能是您可以使用 UIButton 做的事情吗? UIButton 具有所有这些状态,并且可能更容易使用,特别是在您点击它之后确实需要发生某些事情的情况下。最坏的情况是你有一个 UIButton 在点击时不会触发任何方法。
以下是我在 IOS 9 中使用色调和色调不透明度的方式 Swift -
//apply a color to an image
//ref -
//ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming
func getTintedImage() -> UIImageView {
var image : UIImage
var imageView : UIImageView
image = UIImage(named: "someAsset")!
let size : CGSize = image.size
let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height)
let redCover : UIView = UIView(frame: frame)
redCover.backgroundColor = UIColor.redColor()
redCover.layer.opacity = 0.75
imageView = UIImageView()
imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic)
imageView.addSubview(redCover)
return imageView
}