繁体   English   中英

iOS / Objective-C:更改图像视图的色彩

[英]IOS/Objective-C: Change tint of an image view

我想在用户触摸时更改图像视图的颜色。 我不需要很大的改变。 但是,我希望将其更改为蓝色阴影。 我发现以下代码可更改渐变,但没有任何效果。 实际上,我不需要渐变色。 将不胜感激任何建议。

 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];

您可以尝试给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;

然后,当用户使用轻击手势识别器触摸图像时,更改该色调

一种简单且非侵入式的方法是向图像视图添加半透明的覆盖层。 然后可以在按下和释放按钮时显示和隐藏它。

- (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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM