Question

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?

Was it helpful?

Solution

You should use CACurrentMediaTime(). I've got a similar issue, take a look at this answer

OTHER TIPS

"Animate in a sequence..." Hmm, that sounds like a CAAnimationGroup.

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

CAAnimationGroup requires that you be doing explicit layer animation, not view animation. But you are doing explicit layer animation, so you're all set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top