Search code examples
iosobjective-canimationbeginanimations

Thing doesn't animate back


I want to animate and slowly dim the background when I bring up a picker. And then I want to slowly animate it back when I bring it down. For some reason it just works when I bring up the pickerView, not when a want to bring it down.

This code works:

-(IBAction)chooseCategory:(id)sender{
    [self.chooseCategoryView setHidden:NO];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    self.categoryPicker.frame = CGRectMake(0, 151, 320, 367);
    self.categoryPickerToolbar.frame = CGRectMake(0, 106, 320, 45);
    self.pickerViewBackground.alpha = 0.6;
    [UIView commitAnimations];
    [self.scrollView setUserInteractionEnabled:NO];
}

But this doesn't

-(IBAction)hideCategory:(id)sender {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    self.pickerViewBackground.alpha = 0;
    self.categoryPicker.frame = CGRectMake(0, 545, 320, 367);
    self.categoryPickerToolbar.frame = CGRectMake(0, 500, 320, 367);
    [UIView commitAnimations];
    [self.chooseCategoryView setHidden:YES];
    [self.scrollView setUserInteractionEnabled:YES];
}

Anyone knows why it doesn't?


Solution

  • You are hiding the categoryView right after commitAnimations. So it will be hidden at the very beginning and you will not be able to see the animations.

    You can either use the "old way" with beginAnimations etc. and use the setAnimationDidStopSelector: and hide the view there.

    Or you use the up-to-date way by employing blocks:

    [UIView animateWithDuration:0.2
     animations:^{
        // your animations
     }
     completion:^(BOOL finished){ 
        // hide the view
     }];