将视图从一个位置设置为另一个位置

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"