动画UIImageView色调

时间:2009-12-15 21:37:51

标签: iphone core-animation uiimageview

this previous question类似,我想对任意图像(UIImageView)应用任意颜色的色调。但是,我希望色调在N秒内消失,以产生“脉冲”效果。我会每M秒触发一次脉冲。

虽然我认为我可以使用NSTimer修改一个天真的解决方案来改变色调,但我想我可以使用一些Core Animation框架来获得更优雅(和更有效)的解决方案。但是,我对Core Animation不是很熟悉。

使用Core Animation创建这样的“脉冲”吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分: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"];

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

最好的问候。

更新:找到another SO帖子,说明在CA中进行混合取决于图层的“不透明”属性。