如何同步几个不同层的CAKeyframeAnimation动画?

时间:2014-04-03 15:54:29

标签: ios objective-c core-animation cakeyframeanimation

我需要为5个视图的移动设置动画,每个视图都会从前一个视图开始延迟。我已经开始制作一个视图的动画了:

// Create position points
NSArray * pathArray = @[
                        [NSValue valueWithCGPoint:CGPointMake(0, 0)],
                        [NSValue valueWithCGPoint:CGPointMake(50, 0)],
                        [NSValue valueWithCGPoint:CGPointMake(80, 0)],
                        [NSValue valueWithCGPoint:CGPointMake(130, 0)]
                        ];

// Create animation 
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

pathAnimation.values = pathArray;

// Add relative timing for each position
pathAnimation.keyTimes = [NSArray arrayWithObjects:
                      [NSNumber numberWithFloat:0],
                      [NSNumber numberWithFloat:.2],
                      [NSNumber numberWithFloat:.8],
                      [NSNumber numberWithFloat:1.0], nil];

// Define animation type for each frame
pathAnimation.timingFunctions = [NSArray arrayWithObjects:
                             [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn], // from keyframe 1 to keyframe 2
                             [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], // from keyframe 2 to keyframe 3
                             [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn], nil]; // from keyframe 3 to keyframe 4

// Set duration for whole animation
pathAnimation.duration = 1.0;

// Perform repeat
pathAnimation.repeatCount = HUGE_VALF;

// Add animation
CALayer *layer = _myView.layer;
[layer addAnimation:pathAnimation forKey:@"position"];

现在我需要以某种方式使用相同动画但又有延迟的4个视图,这样所有5个视图都会在序列中生成动画。例如,我需要在1秒后设置第2个视图的动画,然后在1秒后设置第3个视图的动画,依此类推。应该如何正确地完成?

2 个答案:

答案 0 :(得分:2)

您应该使用CACurrentMediaTime()。我有类似的问题,请看一下answer

答案 1 :(得分:1)

"按序列动画..."嗯,这听起来像是CAAnimationGroup。

https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CAAnimationGroup_class/Introduction/Introduction.html

CAAnimationGroup要求您进行显式图层动画,而不是视图动画。但是你 做了明确的图层动画,所以你已经全部设置了。