Search code examples
iosobjective-ccore-animation

Core animation returns layer to the original position when label's text is changed


I've got simple animation which moves my view's layer and remains in it's final state.

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

But when I change a label's text the layer jumps to it's original position.

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

Why does it happen?


Solution

  • Because,when you change your text,the view need to redraw,

    If you enable autoLayout of this view(default is enabled), it will adjust frame to autolayout.

    So there are to solutions

    Option 1: Disable auto layout of your view

    Option 2 When it comes to frame changed animation, try to animate autolayout