动画 UIImageView 色调

Sha*_*rog 5 iphone core-animation uiimageview

与上一个问题类似,我想将任意颜色的色调应用于任意图像(a UIImageView)。然而,我希望色调在 N 秒的时间内逐渐消失,以产生“脉冲”效果。我将每 M 秒触发一次脉冲。

虽然我认为我可以使用一个简单的解决方案NSTimer来改变色调,但我想我可能可以使用一些核心动画框架来获得一个更优雅(和高效)的解决方案。不过,我对核心动画不是很熟悉。

使用核心动画可以创建这样的“脉冲”吗?如果是这样,怎么办?

Mat*_*ong 1

如果您计划使用核心动画,该方法将与您之前的 SO 问题的链接中演示的方法不同。相反,创建一个使用您想要的颜色作为色调的图层,然后为该图层的不透明度设置动画。像这样的东西:

CALayer *tintLayer = [CALayer layer];

// Center the layer on the view
[tintLayer setBounds:[imageView bounds]];
[tintLayer setPosition:CGPointMake([imageView bounds].size.width/2.0, 
                                   [imageView bounds].size.height/2.0)];

// Fill the color
[tintLayer setBackgroundColor:
           [[UIColor colorWithRed:0.5 green:0.5 blue:0.0 alpha:1.0] CGColor]];
[tintLayer setOpacity:0.5];
[[imageView layer] addSublayer:tintLayer];

// Add the animation
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[animation setFromValue:[NSNumber numberWithFloat:0.5]];
[animation setToValue:[NSNumber numberWithFloat:0.0]];
// Animate back to the starting value automatically
[animation setAutoreverses:YES];
// Animate over the course of 5 seconds.
[animation setDuration:5.0];

[tintLayer addAnimation:animation forKey:@"opacity"];
Run Code Online (Sandbox Code Playgroud)

我不确定的唯一问题是它是否会起到相同的作用,因为我们没有在色调层中指定混合模式。我将不得不查看 CA 默认情况下发生的混合情况。

此致。

更新:发现另一篇 SO帖子说 CA 中的混合取决于图层的“不透明”属性。