Search code examples
iphoneiosuianimation

Animation not Shown When Calling it First Time


I have one simple animation:

- (void)showMenu
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    self.scrollView.frame = CGRectMake(296, -150, 432, 356);

    [self.buttonMenu setImage:[UIImage imageNamed:@"menu_arrow_up.png"] forState:UIControlStateNormal];
    [self.view bringSubviewToFront:self.scrollView];

    [UIView commitAnimations];
}

When I call this animation the first time, just the image of my button changes, the position of my scrollview does not change.

Everything works correctly, after I called my showMenu method twice.

  • I call showMenu. the image of the button changes. position of scrollview not
  • I call another method to reset everything. image of button changes. position of scrollview not
  • I call showMenu. the image of the button changes. position of scrollview changes
  • from now on it works every time I call showmenu

the second method is closeMenu:

- (void)closeMenu
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    self.scrollView.frame = CGRectMake(296, -317, 432, 356);

    [self.buttonMenu setImage:[UIImage imageNamed:@"menu_arrow_down.png"] forState:UIControlStateNormal];

    [UIView commitAnimations];
}

So why does it work just when I click it twice?


Solution

  • I had the same problem when i perform a UIAnimation and simultaneously adding a UITableView programmatically. The frame of UIView changes but it will not update in display.

    To solve this problem call your method using performSelector:withObject:afterDelay: with a delay of 0.