How to implement highlighting on UIImage like UIButton does when tapped?(如何在 UIImage 上像 UIButton 一样在点击时实现突出显示?)
问题描述
我需要复制 UIButton 在点击时对图像的效果,即突出显示.见:
I need to replicate the effect that the UIButton does on an image when tapped, the highlighting. See:
原始 PNG 是一个带有 alpha 背景的正方形.当我将它设置为 UIButton 的图像时,它会自动对图像的非 alpha 像素应用效果.
The original PNG is a square with alpha background. When I set it as UIButton's image it automatically apply an effect on the non-alpha pixels of the image.
这个效果怎么做?
推荐答案
您可以通过 UIImage 上的简单类别来实现:
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
对于上面显示的效果,您需要传递类似 [UIColor colorWithWhite:0.0 alpha:0.3]
作为 tintColor 参数(使用 alpha 值进行实验).
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).
这篇关于如何在 UIImage 上像 UIButton 一样在点击时实现突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 UIImage 上像 UIButton 一样在点击时实现突出显示?


- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- UITextView 内容插图 2022-01-01
- URL编码Swift iOS 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01