[英]IOS/Objective-C: Change tint of an image view
I would like to change the color of an image view when the user touches it. 我想在用户触摸时更改图像视图的颜色。 I don't need it to change a great deal.
我不需要很大的改变。 However, I would like it to change to a shade of blue.
但是,我希望将其更改为蓝色阴影。 I found the following code to change the gradient but it is not having any effect.
我发现以下代码可更改渐变,但没有任何效果。 I don't need a gradient, actually, only a tint.
实际上,我不需要渐变色。 Would appreciate any suggestions.
将不胜感激任何建议。
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blackColor].CGColor];
[snapshot.layer insertSublayer:gradient atIndex:0];
You can try giving the UIImage a tint 您可以尝试给UIImage着色
UIImage *image = [[UIImage imageNamed:@"Your Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tintColor = [UIColor clearColor]; // Or any color really you'd like would work
imageView.image = newImage;
And then changing that tint when the user touches the image using a tap gesture recognizer 然后,当用户使用轻击手势识别器触摸图像时,更改该色调
An easy and non-intrusive way is to add a translucent overlay to your image view. 一种简单且非侵入式的方法是向图像视图添加半透明的覆盖层。 This can then be shown and hidden as the button is pressed and released.
然后可以在按下和释放按钮时显示和隐藏它。
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.bounds];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.3]; // Your color overlay goes here
[imageView addSubview:overlay];
}
overlay.hidden = !on;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.