iOS - Parabolic Animation using CoreAnimation
this code is just another example how to create parabolic animation
using CoreAnimation CAKeyframeAnimation
.
- (void)parabolicAnimationWithCallback:(void(^)())callback {
[CATransaction begin];
[CATransaction setCompletionBlock:^{
CGPoint temp = view1.center;
view1.center = view2.center;
view2.center = temp;
if (callback) callback();
}];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 1.;
CGPoint fromPoint = view1.center;
CGPoint toPoint = view2.center;
BOOL isClockwise = YES;
CGPoint midPoint = CGPointZero;
midPoint.x = fromPoint.x + ((toPoint.x - fromPoint.x) / 2);
midPoint.y = fromPoint.y + ((toPoint.x - fromPoint.x) / 2 * (isClockwise ? -1 : 1));
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, fromPoint.x, fromPoint.y);
CGPathAddCurveToPoint(curvedPath, NULL, fromPoint.x, fromPoint.y, midPoint.x, midPoint.y, toPoint.x, toPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
[view1.layer addAnimation:pathAnimation forKey:@"animateAlongPath"];
[CATransaction commit];
}
Written by Noval Agung Prayogo
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ios
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#