【问题标题】:How to coordinate animation times on multiple CALayers?如何在多个 CALayers 上协调动画时间?
【发布时间】:2010-06-09 18:42:16
【问题描述】:
您好,我有两个 CALayer(*layer1 和 *layer2)。我想要的是当 layer1 的动画时间的 50% 已经过去时,在 layer2 上开始动画。我怎样才能做到这一点?
我尝试使用 CAAnimationGroup,但这仅适用于同一层中的动画。我找到了一种解决方法,您必须首先将延迟的动画添加到组中并设置 beginTime 属性,但这对我来说似乎有点骇人听闻,我想知道是否有正确的方法来实现我想要的。
谢谢!
【问题讨论】:
标签:
iphone
core-animation
【解决方案1】:
将第二个动画添加到组中的方法确实有点像 hack,但它有效并且完全可以接受。但是,如果您愿意,您可以调用 -performSelector:withObject:afterDelay。像这样的:
- (void)startFirstAnimation;
{
CGFloat duration = 10.0;
CABasicAnimation *firstAnim =
[CABasicAnimation animationWithKeyPath:@"position"];
[firstAnim setFromValue:
[NSValue valueWithCGPoint:CGPointMake(30.0f, 30.0f)]];
[firstAnim setToValue:
[NSValue valueWithCGPoint:CGPointMake(200.0f, 200.0f)]];
[firstAnim setDuration:duration];
[firstLayer addAnimation:firstAnim forKey:nil];
[self performSelector:@selector(startSecondAnimation)
withObject:nil afterDelay:duration/2.0];
}
- (void)startSecondAnimation;
{
CABasicAnimation *secondAnim =
[CABasicAnimation animationWithKeyPath:@"position"];
[secondAnim setFromValue:
[NSValue valueWithCGPoint:CGPointMake(100.0f, 30.0f)]];
[secondAnim setToValue:
[NSValue valueWithCGPoint:CGPointMake(200.0f, 200.0f)]];
[secondAnim setDuration:5.0];
[secondLayer addAnimation:secondAnim forKey:nil];
}