Animate a view from one position to another

suggest change

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)
animation.duration = 1

_label.layer.addAnimation(animation, forKey: "basic")

The layer’s presentation moves from 0 to 320 horizontally, then snaps back. A CABasicAnimation does not change the model value. Set position (or use fillMode with isRemovedOnCompletion = false) if the view should stay at the end position.

To move vertically, use this key path instead:

"position.y"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents