使用Quartz 2D或CALayer动画制作“红色警报”动画效果?

时间:2011-12-08 20:50:41

标签: ios animation calayer quartz-2d

我是动画的新手,我找不到有关如何创建警示灯的数据(简单的红色警示灯闪烁)。

我知道如何为CALayer制作动画以及Quartz 2D的基础知识,我只是在寻找关于如何实现背光效果的教程或方向?

由于 沙尼

1 个答案:

答案 0 :(得分:0)

非常hacky代码(有效但尝试不在生产中使用它):

self.uiView1.backgroundColor = [UIColor redColor];

CABasicAnimation* selectionAnimation1 = [CABasicAnimation 
                                        animationWithKeyPath:@"opacity"];

selectionAnimation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

selectionAnimation1.fromValue = [NSNumber numberWithFloat:0.0];
selectionAnimation1.toValue = [NSNumber numberWithFloat:1.0];

selectionAnimation1.duration = 0.10;
selectionAnimation1.repeatCount = 20;

[self.uiView1.layer addAnimation:selectionAnimation1
                       forKey:@"opacity"];


self.uiView2.backgroundColor = [UIColor blackColor];

CABasicAnimation* selectionAnimation2 = [CABasicAnimation 
                                        animationWithKeyPath:@"opacity"];

selectionAnimation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

selectionAnimation2.fromValue = [NSNumber numberWithFloat:1.0];
selectionAnimation2.toValue = [NSNumber numberWithFloat:0.0];

selectionAnimation2.duration = 0.10;
selectionAnimation2.repeatCount = 20;

[self.uiView2.layer addAnimation:selectionAnimation2
                          forKey:@"opacity"];

uiView1& uiView2完全相互重叠。您可以用两个UIImageView替换两个uiView,其中一个具有打开的光图像(红色),另一个具有关闭的光(黑色/灰色)。在这种情况下,背景颜色变化将是多余的。