2014-04-03 43 views
0

我需要为5个视图的移动设置动画,每个视图都从前一个延迟开始。我心中已经早已见怪不怪工作一个视图的动画:如何同步几个不同图层的CAKeyframeAnimation动画?

// 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个视图,依此类推。这应该如何正确完成?

回答

1

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

1

“动画序列中......”嗯,这听起来像一个CAAnimationGroup。

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

CAAnimationGroup要求您做明确的层动画,无法查看动画。但你做明确的层动画,所以你都设置。

+0

看第一个例子,我这​​个话题讨论:http://www.apeth.com/iOSBook/ch17.html#_grouped_animations我旋转箭头,_then_来回动箭头 - 两个完全不同的动画_in sequence_。 – matt

+1

动画组仅在组中的所有动画应用于同一图层时才起作用。 –