当标签的文本发生变化时,核心动画会将图层返回到原始位置

时间:2015-05-04 08:26:31

标签: ios objective-c core-animation

我有一个简单的动画,可以移动我的视图层,并保持最终状态。

CGPoint originalPosition = self.redSquare.layer.position;
self.redSquare.layer.position   = CGPointMake(100, 100);
CABasicAnimation *animation = [[CABasicAnimation alloc]init];
animation.keyPath = @"position.x";
animation.fromValue = @(originalPosition.x);
animation.toValue = @250;
animation.duration = 2;

[self.redSquare.layer addAnimation:animation forKey:@"basic"];
self.redSquare.layer.position = CGPointMake(250, originalPosition.y);

但是当我更改标签的文字时,图层会跳转到原来的位置。

CGPoint originalPosition = self.redSquare.layer.position;
self.redSquare.layer.position   = CGPointMake(100, 100);
CABasicAnimation *animation = [[CABasicAnimation alloc]init];
animation.keyPath = @"position.x";
animation.fromValue = @(originalPosition.x);
animation.toValue = @250;
animation.duration = 2;
animation.delegate = self;

[self.redSquare.layer addAnimation:animation forKey:@"basic"];
self.redSquare.layer.position = CGPointMake(250, originalPosition.y);

self.label.text = @"Started";

为什么会这样?

1 个答案:

答案 0 :(得分:0)

因为,当您更改文本时,视图需要重绘,

如果启用此视图的autoLayout(默认为启用),则会将帧调整为自动布局。

所以有解决方案

选项1: 禁用视图的自动布局

选项2 当谈到帧改变的动画时,尝试动画自动布局