如何让一个CABasicAnimation在另一个完成后运行?换句话说,是按顺序进行的。我已经添加了第二个动画的开始时间,但是第二个动画似乎没有执行:
CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
appearance.duration = 0.5;
appearance.fromValue = [NSNumber numberWithFloat:0];
appearance.toValue = [NSNumber numberWithFloat:340];
appearance.repeatCount = 1;
appearance.fillMode = kCAFillModeForwards;
appearance.removedOnCompletion = NO;
[notif.layer addAnimation:appearance forKey:@"transform.translation.y"];
CABasicAnimation * theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
theAnimation.duration = 0.5;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:10];
theAnimation.repeatCount = 3;
theAnimation.autoreverses = YES;
theAnimation.fillMode = kCAFillModeForwards;
theAnimation.removedOnCompletion = NO;
theAnimation.beginTime = appearance.beginTime + appearance.duration;
[notif.layer addAnimation:theAnimation forKey:@"transform.translation.y"];知道为什么吗?
发布于 2011-10-17 09:27:27
已更新代码,这将会起作用!
动画
CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
[appearance setValue:@"animation1" forKey:@"id"];
appearance.delegate = self;
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
appearance.duration = 0.5;
appearance.fromValue = [NSNumber numberWithFloat:0];
appearance.toValue = [NSNumber numberWithFloat:340];
appearance.repeatCount = 1;
appearance.fillMode = kCAFillModeForwards;
appearance.removedOnCompletion = NO;
[notif.layer addAnimation:appearance forKey:@"transform.translation.y"];第二部动画:
- (void)animationDidStop:(CAAnimation *)theAnimation2 finished:(BOOL)flag {
if([[theAnimation2 valueForKey:@"id"] isEqual:@"animation1"]) {
CABasicAnimation * theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
theAnimation.duration = 0.5;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:10];
theAnimation.repeatCount = 3;
theAnimation.autoreverses = YES;
theAnimation.fillMode = kCAFillModeForwards;
theAnimation.removedOnCompletion = NO;
theAnimation.beginTime = appearance.beginTime + appearance.duration;
[notif.layer addAnimation:theAnimation forKey:@"transform.translation.y"];
}}
这就是在第一个动画结束后,第二个动画将如何触发。
发布于 2011-10-17 08:24:46
要做的最简单的事情是为第一个动画设置一个代理,并在该对象中实现-animationDidStop:finished:。在该方法中,触发第二个动画。
发布于 2011-10-17 10:56:22
你的动画没有按照计划运行的原因是由于你编码的方式&使用核心动画。核心动画都是由GPU驱动的。事实上,CA很好地处理了一些事情,以便以最佳方式呈现动画。
它的一个元素是现代图形卡的原始并行处理能力,另一个是核心动画可以在卡上缓存CALayer的内容,以便您的代码不需要不断地重新绘制它。顺便说一句,这是iPhone UI在一些相对普通的硬件上如此令人难以置信地快速的部分原因。核心动画可以自动利用多核Mac,因为层树在上呈现为单独的线程。
最后一行是imp。CA在单独的线程(&不是主线程)中运行。所以回到你的问题上来,你有两个CA块,每个块都试图同时为相同的transform.translation.y设置动画!这是如何工作的?
所以要解决这个问题,要么-
,
我只想强调核心动画工作方式背后的理论和推理。希望这能帮到你。
https://stackoverflow.com/questions/7788289
复制相似问题