简体   繁体   中英

Animation along path - Completion method called too early

I'd like to implement an animation along a rounded path and fire a completion function as soon as the animation has ended.

Thanks to this post I figured out how to use a CAKeyframeAnimation with a CGMutablePathRef to perform the animation along a path.

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 1.0;

CGPoint endPoint = CGPointMake(self.boardReference.view.bounds.size.width/2,self.boardReference.view.bounds.size.height/2);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, self.view.center.x, self.view.center.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, self.view.center.y, endPoint.x, self.view.center.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
[self.view.layer addAnimation:pathAnimation forKey:@"curvedPath"];

[self addNewCardAtPoint:endPoint]; //This should only be launched after the animation has been finished

Problem: My method addNewCardAtPoint should only be called after the animation has been completed. Currently the process is not blocking so the method is called more or less simultaneously.

At the first view the animateWithDuration function seems to better suited as it has defined blocks for the animation and the completion action. But simply adding the CAKeyframeAnimation to the animations block has basically the same result, immediate execution of the completion block.

[UIView animateWithDuration:0.8
                 animations:^{
                     NSLog(@"-Animate on curved path to end point-");
                     ... //how perform the same path based animation here?
                 } completion:^(BOOL finished) {
                     [self addNewCardAtPoint:self.view.center];
                 }
 ];

How can I fire a complete function only after the animation along the curved path has been completed?

完成CAAnimation后,它将在其委托上调用animationDidStop:finished:方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM