Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation Blocks resets to original position after updating text

I'm currently testing my apps for the release of IOS 8. I noticed that after I performed an animation block, the animation resets if I update the text of any label. I ran a simple example with one method shown below. Running this example results in the following:

  • Clicking myButton the first time- animation runs but resets when the label text is changed.
  • Clicking myButton the second time - animation runs but does not reset to original position.

It seems like this happens because the label text doesn't change. If I completely remove the line updating the text, this also stops the animation from resetting at the end.

I would like to fix this so that when the method runs, the label text can be updated without resetting the animation.

- (IBAction)move:(id)sender {

[UIView animateWithDuration:0.4 delay:0.0
                 options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     self.myButton.center = CGPointMake(200, 300);
                 }completion:^(BOOL finished){

                     if(finished){
                         self.myLabel.text=@"moved";
                     }

                 }];
}
like image 556
CrazyBae Avatar asked Sep 10 '14 19:09

CrazyBae


2 Answers

This problem can be caused by having Auto Layout set on the UIView. Strictly speaking, if you're using Auto Layout, then you shouldn't animate the absolute position of objects -- you should animate their constraints instead.

Changing the label text once your animation is underway triggers a layout refresh, and iOS shuffles everything around to comply with the original view constraints. (I suspect this is a behavioural change from iOS7).

Quick fix: un-check Auto Layout on the View, and this should work as expected.

like image 161
John Shepherd Avatar answered Nov 04 '22 04:11

John Shepherd


Try this. Put the desired animation in the finish block also.

    - (IBAction)move:(id)sender {

    [UIView animateWithDuration:0.4 delay:0.0
                     options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{


self.myButton.center = CGPointMake(200, 300);
                 }completion:^(BOOL finished){



         if(finished){
                         self.myLabel.text=@"moved";
 self.myButton.center = CGPointMake(200, 300);
                     }

                 }];
}
like image 1
Jarek Toro Avatar answered Nov 04 '22 06:11

Jarek Toro