xcode 移动一个 CALayer(添加动画)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8316360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:36:13  来源:igfitidea点击:

move a CALayer (add animation)

iphonexcodeanimationuiimageviewcalayer

提问by jean bernard

well I have a CALayer layerand I would like to move it, with a CADisplaylink. Like :

好吧,我有一个 CALayer layer,我想用 CADisplaylink 移动它。喜欢 :

layer.center=CGPointMake(layer.center.x + 10, layer.center.y + 10);

but I can't use centeror positionfor the layer.Here is my problem, I want to make it move like it was a uiimageview.

但我不能使用centerposition用于图层。这是我的问题,我想让它像 uiimageview 一样移动。

回答by beryllium

To move layer try to use this method

要移动图层尝试使用此方法

-(void)moveLayer:(CALayer*)layer to:(CGPoint)point{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [layer valueForKey:@"position"];
    animation.toValue = [NSValue valueWithCGPoint:point];
    layer.position = point;
    [layer addAnimation:animation forKey:@"position"];
}