I need to animate the move of 5 views, each of them starts with a delay from previous one. I'v already got working animation of one view:
// 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"];
Now I need somehow to use 4 more Views with the same animations but with delay, so that all 5 views would animate in a sequence. For example, I need to animate 2nd view after 1 second, then animate 3rd view after 1 second and so on. How that should be done correctly?
You should use CACurrentMediaTime()
. I've got a similar issue, take a look at this answer