2

I was hoping to make my UIImage "highlight" briefly upon being tapped. Not sure of the color yet, but let's say blue for arguments sake. So you tap the image, it briefly looks blue and then it navigates you to a details page to edit something on another screen.

From some initial reading it seems the right course of action is to use the Quartz framework and do this:

   imageView.layer.backgroundColor = UIColor.blueColor()
   imageView.layer.opacity = 0.7

I guess the idea would be you change the background of the layer behind the image, and then by setting the opacity of the image, the blue "bleeds through" a little bit, giving you a slightly blue image?

When I try the above, however, a blue border goes around the image itself, and based upon the opacity, the blue is either dark or light. The actual image does not become any more blue, but it does react to the opacity (meaning if I set it to something like .1, the image is very faded and barely visible). Why does the image react correctly, but not show blue?

Thanks so much!

NullHypothesis
  • 4,286
  • 6
  • 37
  • 79

4 Answers4

6

As far as I know changing the opacity will change the opacity for the WHOLE view, meaning not just the UIImage that the UIImageView holds. So instead of fading to reveal the UIImageView's background color, instead the opacity of the whole view is just decreased as you're seeing.

Another way you could do it though would be to add an initially transparent UIView on top of your UIImageView and change its opacity instead:

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
}];
J2K
  • 1,593
  • 13
  • 26
2

Here's how I use tint and tint opacities in IOS 9 with Swift -

//apply a color to an image
//ref - http://stackoverflow.com/questions/28427935/how-can-i-change-image-tintcolor
//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
}
J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
0

Is this tap highlight perhaps something you could do with a UIButton? UIButton has all these states off the bat and might be a bit easier to work with, specially if there's something that actually needs to happen after you tap it. Worst case scenario is you have a UIButton that does not trigger any method when tapped.

Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
-1

You can try changing the tint color instead:

    UIImage *image = [UIImage imageNamed:@"yourImageAsset"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    imageView.tintColor = [UIColor blueColor];
    imageView.image = image;
raf
  • 2,569
  • 20
  • 19