How to implement highlighting on UIImage like UIButton does when tapped?

前端 未结 1 1146
梦如初夏
梦如初夏 2020-12-12 16:39

I need to replicate the effect that the UIButton does on an image when tapped, the highlighting. See:

\"alt

相关标签:
1条回答
  • 2020-12-12 16:59

    You could achieve this with a simple category on UIImage:

    @interface UIImage (Tint)
    
    - (UIImage *)tintedImageUsingColor:(UIColor *)tintColor;
    
    @end
    
    @implementation UIImage (Tint)
    
    - (UIImage *)tintedImageUsingColor:(UIColor *)tintColor {
      UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
      CGRect drawRect = CGRectMake(0, 0, self.size.width, self.size.height);
      [self drawInRect:drawRect];
      [tintColor set];
      UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
      UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return tintedImage;
    }
    
    @end
    

    For the effect shown above, you'd pass something like [UIColor colorWithWhite:0.0 alpha:0.3] as the tintColor parameter (experiment with the alpha value).

    0 讨论(0)
提交回复
热议问题