iOS CAAnimation Animate a view from one position to another.

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Objective-C

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
animation.fromValue = @0;
animation.toValue = @320;
animation.duration = 1;
                
[_label.layer addAnimation:animation forKey:@"basic"];

Swift

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"


Got any iOS Question?