簡體   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