【发布时间】:2014-04-03 15:54:29
【问题描述】:
我需要为 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 秒后为第二个视图设置动画,然后在 1 秒后为第三个视图设置动画,以此类推。应该如何正确完成?
【问题讨论】:
标签: ios objective-c core-animation cakeyframeanimation