2
votes

I am animating a view that is sliding down. In the view there is a simple UIButton. The view and the button have fixed widths and heights (I checked with adding a color as background). Although when use:

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //Slide Down the notification
        CGRect notificationsRect = self.notificationContainerView.bounds;
        notificationsRect.origin.y = 0;
        notificationViewController.view.frame = notificationsRect;
    } completion:^(BOOL finished) {
        [notificationViewController didMoveToParentViewController:self];
        self.currentNotification = notificationViewController; 
}];

then the view slides down perfectly, expect for that the text in the UIButton kind of fades in. It starts really small and animates to the correct font size.

How can I disable the animation of the text in the UIButton?

3
I don't understand about "It starts really small and animates to the correct font size.". "animates to the correct font size"? - tuledev
The font size starts at lets say 4pt and then animates to font size 14pt. The button size however does not change.. - Ruud Visser
Can you show more about the button? - tuledev
The button is just a UIButton as a subview of the notificationContainerView. I did nothing with it. - Ruud Visser
As a workaround, subclass the button, and wrap the layoutSubviews method with [UIView performWithoutAnimations: ^ {. Let me know if this helps. - Léo Natan

3 Answers

7
votes

Use the performWithoutAnimation: method and then force layout to happen immediately instead of later on.

[UIView performWithoutAnimation:^{
  [self.myButton setTitle:text forState:UIControlStateNormal];
  [self.myButton layoutIfNeeded];
}]; 
0
votes

Is the effect you have something similar to what is below? I tried to imitate your situation by having a view with a UIButton inside with some some text set for the title of the button. I then animated the button similarly to the way you did. As you can see there isn't really a fade happening on the text of my button inside my view that is sliding so I wonder if there is something else at play for you. I have the code I used below also to mock the situation.

enter image description here

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)];
    self.myView.backgroundColor = [UIColor greenColor];
    UIButton *myButton= [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 80, 80)];
    myButton.backgroundColor = [UIColor blueColor];
    [myButton setTitle:@"fun times" forState:UIControlStateNormal];
    [self.myView addSubview:myButton];
    [self.view addSubview:self.myView];

    UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 300, 50, 50)];
    resetButton.backgroundColor = [UIColor blackColor];
    [resetButton addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];

    UIButton *goButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 100, 50, 50)];
    goButton.backgroundColor = [UIColor redColor];
    [goButton addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:goButton];
    [self.view addSubview:resetButton];
}

- (void)reset {
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //Slide Up the notification
        CGRect notificationsRect = self.myView.bounds;
        notificationsRect.origin.y = 0;
        notificationsRect.origin.x = 200;
        self.myView.frame = notificationsRect;
    } completion:nil];
}

- (void)go {
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //Slide Down the notification
        CGRect notificationsRect = self.myView.bounds;
        notificationsRect.origin.y = 200;
        notificationsRect.origin.x = 200;
        self.myView.frame = notificationsRect;
    } completion:nil];
}