Animate a view from one position to another
suggest changeObjective-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"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents