如何在另一个CABasicAnimation完成后运行一个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"];
知道为什么吗?
答案 0 :(得分:15)
更新了代码,这将有效!
第一个动画
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"];
}
}
这是第一个动画完成后第二个动画的触发方式。
答案 1 :(得分:1)
最简单的方法是为第一个动画设置一个委托,并在该对象中设置-animationDidStop:finished:
。在该方法中,触摸第二个动画。
答案 2 :(得分:1)
您的动画未按计划运行的原因是由于您编码的方式和使用了Core Animation。核心动画都是GPU驱动的。 CA infact可以很好地处理几件事情,以最佳方式呈现动画。
它的一个要素是现代图形卡的原始并行处理能力,另一个因素是Core Animation可以在卡上缓存CALayer的内容,这样您的代码就不需要不断重绘它。顺便说一下,这是iPhone UI在一些相对适中的硬件上非常快速的部分原因。 Core Animation可以自动利用多核Mac,因为层树在单独的线程上呈现。
最后一行是imp。 CA正在一个单独的线程中运行(而不是主线程)。所以回到你的问题,你有2块CA,每块都试图同时为transform.translation.y
两个动画制作动画!这将如何运作?
所以要解决这个问题,要么做 -
我只是想突出理论和核心动画工作方式背后的推理。希望这会有所帮助...
答案 3 :(得分:0)
最简单,最干净的方法是使用CAAnimationGroup
。看看这个答案 - 它帮助了我。