Search code examples
iosobjective-ciphoneuilabeluianimation

Setting a UILabel while in the middle or end of an animation from a UIButton action cancels the animation


As the title says, whenever I want to set the text of a UILabel after the animation of the movement of the label, it returns the UILabel back to the origin before the animation. What's interesting is that this only happens when the animation is triggered by a UIButton action.

I've replicated this in a new project. Copy the below code and see what happens.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *hello;

- (IBAction)move:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.hello.text = @"Hello";

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)move:(id)sender {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        self.hello.text = @"Howdy";
    });

    [UIView animateWithDuration:1.0f animations:^{
        self.hello.center = CGPointMake(self.hello.center.x + 50, self.hello.center.y + 50);
    }];
}

@end

Where self.hello and move are linked to a UILabel and UIButton in the storyboard.

Playing around with this I can see that this error does not occur when instead of using the self.hello.center, you use self.hello.transform = CGAffineTransformMakeTranslation(50, 50). Why is this? Is there any way to continue using .center or do I have to change all of my animations to .transform?

Thanks for your help.


Solution

  • I've had to give into the ways of .transform