Search code examples
iphoneobjective-ciosxcode4

Beginner: Multiview app doesn't switch views correctly


I wrote a basic two-view application that switches from one view to another every time I press a button.

But for some reason when I run it on simulator, both views are always few pixels above the MainWindow.xib view, always being on top of it. And what's strange is that there is no animation when I switch between Views.

What is the problem???

This is what I have in my AppDelegate.m

-(void)switchView:(UIView *)view1 toView:(UIView *)view2{

    [UIView beginAnimations:@"Animation" context:nil];
    [UIView setAnimationDuration:1.75];
    [UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromLeft forView:self.window cache:YES];
    [view1 removeFromSuperview];
    [window addSubview:view2];
    [UIView commitAnimations];
}

Solution

  • Hi Try This,

     -(void)switchView:(UIView *)view1 toView:(UIView *)view2
        {
            view2.frame = self.window.bounds;
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDuration:0.5f];
            [UIView setAnimationBeginsFromCurrentState:YES];
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:NO];
            [view1 removeFromSuperview];
            [window addSubview:view2];
            [UIView commitAnimations];
        }
    

    Also try this

    [UIView transitionWithView:view2 duration:0.5
            options:UIViewAnimationTransitionFlipFromLeft //change to whatever animation you like
            animations:^ { [window addSubview:view2]; }
            completion:nil];