Search code examples
iphoneiosuiviewuiviewcontrollercore-animation

Animate a UIView change where the UIView is totally rebuilt


My UIViewController gets the view it is controlling to completely rebuild itself with something like this:

[self.view rebuildYourself:bIsLandscape]; // this line is in the ViewController

The view itself then contains the following method:

-(void)rebuildYourself:(BOOL)isLandscape
{
    NSArray *viewsToRemove = [self subviews];
    for (UIView *v in viewsToRemove) {
        [v removeFromSuperview];
    }

    [self addControls]; // adds lots of views
    [self layoutControlsWithOrientation:isLandscape]; // frames the views

}

I would like to animate the entire transition. I have tried this:

[UIView animateWithDuration:1.0
                      delay:0.0
                    options:UIViewAnimationOptionTransitionCurlUp
                 animations:^{
                             [self.view rebuildYourself:bIsLandscape];
                         }
                 completion:^(BOOL finished){

                         }];

but the animation ignores the options value of UIViewAnimationOptionTransitionCurlUp and flows in from the top left corner every time.

and I have tried this:

[UIView beginAnimations:@"kkk" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationDuration:1.0];
[self.view rebuildYourself:bIsLandscape];
[UIView commitAnimations];

but in this case the transition does curl up nicely but the underlying view is blank until the transition has finished, and then the new view suddenly 'pops' into view, which spoils the effect of the transition.

Can anyone tell me the right/best way to animate a transition where the view rebuilds itself completely?

Thanks


Solution

  • Make two views and put the old view overtop of the new re-built view. Then 'Curl-up' the old view so that the new view is showing and remove the old view from memory.