CAAnimation
Animate a view from one position to another.
Section titled “Animate a view from one position to another.”Objective-C
Section titled “Objective-C”CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];animation.fromValue = @0;animation.toValue = @320;animation.duration = 1;
[_label.layer addAnimation:animation forKey:@"basic"];let animation = CABasicAnimation(keyPath: "position.x")animation.fromValue = NSNumber(value: 0.0)animation.toValue = NSNumber(value: 320.0)
_label.layer.addAnimation(animation, forKey: "basic")The view will move from 0 to 320 horizontally. if you want to Move view to Vertically just replace keypath like this:
"position.y"Animate View - Toss
Section titled “Animate View - Toss”OBJECTIVE-C
Section titled “OBJECTIVE-C”CATransition* transition = [CATransition animation];transition.startProgress = 0;transition.endProgress = 1.0;transition.type = @"flip";transition.subtype = @"fromLeft";transition.duration = 0.8;transition.repeatCount = 5;[_label.layer addAnimation:transition forKey:@"transition"];var transition = CATransition()transition.startProgress = 0transition.endProgress = 1.0transition.type = "flip"transition.subtype = "fromLeft"transition.duration = 0.8transition.repeatCount = 5label.layer.addAnimation(transition, forKey: "transition")Revolve View
Section titled “Revolve View”CGRect boundingRect = CGRectMake(-150, -150, 300, 300);
CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];orbit.keyPath = @"position";orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(boundingRect, NULL));orbit.duration = 4;orbit.additive = YES;orbit.repeatCount = HUGE_VALF;orbit.calculationMode = kCAAnimationPaced;orbit.rotationMode = kCAAnimationRotateAuto;
[_label.layer addAnimation:orbit forKey:@"orbit"];Shake View
Section titled “Shake View”Objective-C
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];animation.values = @[ @0, @10, @-10, @10, @0 ];animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];animation.duration = 0.4;animation.additive = YES;[_label.layer addAnimation:animation forKey:@"shake"];Swift 3
let animation = CAKeyframeAnimation(keyPath: "position.x")animation.values = [ 0, 10, -10, 10, 0 ]animation.keyTimes = [ 0, NSNumber(value: (1 / 6.0)), NSNumber(value: (3 / 6.0)), NSNumber(value: (5 / 6.0)), 1 ]animation.duration = 0.4animation.isAdditive = truelabel.layer.add(animation, forKey: "shake")Push View Animation
Section titled “Push View Animation”Objective C
Section titled “Objective C”CATransition *animation = [CATransition animation];[animation setSubtype:kCATransitionFromRight];//kCATransitionFromLeft[animation setDuration:0.5];[animation setType:kCATransitionPush];[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];[[yourView layer] addAnimation:animation forKey:@"SwitchToView1"];let animation = CATransition()animation.subtype = kCATransitionFromRight//kCATransitionFromLeftanimation.duration = 0.5animation.type = kCATransitionPushanimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)yourView.layer.addAnimation(animation, forKey: "SwitchToView1")Remarks
Section titled “Remarks”CAAnimation is an abstract animation class. It provides the basic support for the CAMediaTiming and CAAction protocols. To animate Core Animation layers or Scene Kit objects, create instances of the concrete subclasses CABasicAnimation, CAKeyframeAnimation, CAAnimationGroup, or CATransition.