views:

620

answers:

1

Hi all -

I'm trying to build a subview that sits above my main view that is approximately 3/4 the size of the main view. When a button is pressed, the subview should flip to another subview. I'm close, but am having TWO problems.

The first problem is that the first time the flip button is pressed, the second subview starts at full screen and shrinks to the specified frame size rather than any flipping animations being rendered.

Once the second view is rendered, I can then flip between the first and second views (I added a second button to flip back for testing purposes).

My second issue is that when the view flips, the new view is loaded before the flip, and then the flipping occurs. I've built flipping views before that have worked correctly, but only when the entire screen was the view.

Here's my code that is in my main view controller:

- (void)viewDidLoad {

    Page1Controller *myPage1=[[Page1Controller alloc] initWithNibName:@"Page1Controller" bundle:nil];
    Page2Controller *myPage2=[[Page2Controller alloc] initWithNibName:@"Page2Controller" bundle:nil];
    self.page1 = myPage1;
    self.page2 = myPage2;
    [myPage1 release];
    [myPage2 release];
    [[page1 view] setFrame:CGRectMake(45, 40, 230, 280)];
    [[self view] addSubview:[page1 view]];

    [super viewDidLoad];
}

And here is the code for the two buttons:

- (void)flipPressed:(id)sender {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[page2 view] cache:YES];
    [page1.view removeFromSuperview];
    [[page2 view] setFrame:CGRectMake(45, 40, 230, 280)];
    [[self view] addSubview:[page2 view]];

    [UIView commitAnimations];

}

- (void)backFlipPressed:(id)sender {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[page1 view] cache:YES];
    [page2.view removeFromSuperview];
    [[self view] addSubview:[page1 view]];
    [[page1 view] setFrame:CGRectMake(45, 40, 230, 280)];

    [UIView commitAnimations];
}

Any help on either of my issues is greatly appreciated!

A: 

First problem: Set the frames of your views before beginAnimations.

Nikolai Ruhe
Thank you - that fixed the problem with the second subview starting full screen and shrinking. The view still doesn't flip on the first button click, only on subsequent button clicks.
Mike W