如何使UIImageView变暗 [英] how to darken a UIImageView
问题描述
我需要在触摸UIImageView时使其变暗,几乎与跳板(主屏幕)上的图标一样, .
I need to darken a UIImageView when it gets touched, almost exactly like icons on the springboard (home screen).
应该为我添加具有0.5 alpha和黑色背景的UIView.这看起来很笨拙.我应该使用图层还是其他东西(CALayers).
Should I be added UIView with a 0.5 alpha and black background. This seems clumsy. Should I be using Layers or something (CALayers).
推荐答案
如何对UIView进行子类化并添加UIImage ivar(称为image)?然后,您可以覆盖-drawRect:类似这样的内容,只要您在触摸时设置了一个名为被按下的布尔ivar即可.
How about subclassing UIView and adding a UIImage ivar (called image)? Then you could override -drawRect: something like this, provided you had a boolean ivar called pressed that was set while touched.
- (void)drawRect:(CGRect)rect
{
[image drawAtPoint:(CGPointMake(0.0, 0.0))];
// if pressed, fill rect with dark translucent color
if (pressed)
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 0.5);
CGContextFillRect(ctx, rect);
CGContextRestoreGState(ctx);
}
}
您可能要尝试使用上面的RGBA值.而且,当然,非矩形形状需要做更多的工作-例如CGMutablePathRef.
You would want to experiment with RGBA values above. And, of course, non-rectangular shapes would require a bit more work - like a CGMutablePathRef.
这篇关于如何使UIImageView变暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!