將檢視從一個位置設定為另一個位置

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")

檢視將從 0 水平移動到 320。如果你想將檢視移動到垂直只是替換 keypath 像這樣:

"position.y"