Ale*_*lex 19 core-animation objective-c opacity calayer
我想创建一个CALayer动画,给人一种"华而不实"的效果.为此,我试图动画"不透明"属性,但我的问题是我不知道从哪里开始以及如何做到这一点.
以下是动画的图形说明:
opacity
| ___
1 | | |
| | | * repeatCount
0 |___| |_ . . .
-------------------------> time
|______|
duration
Run Code Online (Sandbox Code Playgroud)
不透明度从0开始,然后动画为1,然后再次为0(此0到1到0的动画需要等于持续时间的秒数).然后这个过程重复'repeatCount'次.
以下是代码的一些背景知识:
float duration = ...; // 0.2 secs, 1 sec, 3 secs, etc
int repeactCount = ...; // 1, 2, 5, 6, ect
CALayer* layer = ...; // I have a CALayer from another part of the code
layer.opacity = 0;
// Animation here
done = YES; // IN THE END of the animation set this ivar to yes
Run Code Online (Sandbox Code Playgroud)
完成此任务的最佳方法是什么?我以前从未使用过CALayers,所以这也是了解他们的动画系统如何工作的好机会.顺便说一下,我搜索了文档,我理解你如何添加一两个简单的动画,但我不知道如何做这个特定的动画.
tro*_*foe 46
实现此目的的最佳方法是通过创建实例并将其添加到图层来使用显式动画(请参阅指南)CABasicAnimation
.
代码看起来像这样:
CABasicAnimation *flash = [CABasicAnimation animationWithKeyPath:@"opacity"];
flash.fromValue = [NSNumber numberWithFloat:0.0];
flash.toValue = [NSNumber numberWithFloat:1.0];
flash.duration = 1.0; // 1 second
flash.autoreverses = YES; // Back
flash.repeatCount = 3; // Or whatever
[layer addAnimation:flash forKey:@"flashAnimation"];
Run Code Online (Sandbox Code Playgroud)
如果你想知道动画何时完成,你可以设置一个委托并实现该animationDidStop:finished:
方法,但是最好使用一个完成块,因为它允许所有代码在同一个地方.如果您正在为iOS 4或OS X编写,那么您可以使用优秀的CAAnimationBlocks类别来完成此任务.
归档时间: |
|
查看次数: |
20567 次 |
最近记录: |