简体   繁体   English

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

[英]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.

相关问题 iOS / Objective-C:UIBarButtonItem中的动画图像更改 - IOS/Objective-C: Animate Image Change in UIBarButtonItem iOS - Objective-C:图像处理和处理 - iOS - Objective-C: Image manipulation and processing 更改objective-c中按钮的背景图像? - change the background image of button in objective-c? Objective-C改变影像色彩表现 - Objective-C change image color performance iOS / Objective-C:从共享实例更改View Controller中的属性? - IOS/Objective-C: Change property in View Controller from shared instance? 如何将两个视图合并为一个视图iOS Objective-c(如图所示)? - how to merge two views into one single view iOS objective-c (as shown in the image)? ios / xcode / objective-c:如何在自定义单元格中使用图片创建附件视图? - ios/xcode/objective-c: How to create accessory view with image in custom cell? IOS (Objective-C) 如何从 UIImages 数组无限期更改 UITableView 单元格中的图像 - IOS (Objective-C) How to indefinitely change the image in a UITableView Cell from an Array of UIImages 如何在iOS Objective-C中基于背景图像颜色更改Label / TextField文本颜色? - how to change the Label/TextField text color Based on background image color in iOS Objective-C? 如何从iOS Objective-C中的控制器重绘自定义视图 - how to redraw custom view from the controller in ios objective-c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM