Question

I'm trying to flip animate a view into another view, which corresponds of a small section of the screen. Both views have the same dimensions and center. I keep getting as a result the animation of the full screen. From the code below, can somebody please let me know what the heck i'm doing wrong?

Thank you,

-j

+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(Boolean)wait
{       
    // get parent view
    UIView *parent = [viewA superview];
    CGRect r = viewA.frame;

    // create container view with the same dimensions as ViewA
    UIView *containerView = [[UIView alloc] initWithFrame:viewA.bounds];
    containerView.center = viewA.center;

    // attache both views to intermidiate view
    [containerView addSubview:viewA];
    [containerView addSubview:viewB];

    [viewA removeFromSuperview];

    [parent addSubview:containerView];

    viewB.alpha = 0;
    viewA.alpha = 1;

    // Perform the annimation
    __block BOOL done = NO;

    [UIView transitionWithView:viewA
                      duration:2.0
                       options: (UIViewAnimationOptionTransitionFlipFromTop)
                    animations:^{
                        viewA.alpha = 0;
                        viewB.alpha = 1; }
                    completion:^(BOOL finished) {
                        done = YES;
                        // detach all views
                          [viewA removeFromSuperview];
                          [viewB removeFromSuperview];
                          [containerView removeFromSuperview];
                    }
     ];

    if(wait) {
        while (done == NO)
            [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];

    }
}
Was it helpful?

Solution

First get rid of the old animation code (or at least commit it), it should look like this:

+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(BOOL)wait
{
    viewB.alpha = 0;
    viewA.alpha = 1;

    __block BOOL done = NO;
    [UIView transitionWithView:viewA
                      duration:2.0
                       options: (UIViewAnimationOptionTransitionFlipFromTop)
                    animations:^{
                        viewA.alpha = 0;
                        viewB.alpha = 1; }
                    completion:^(BOOL finished) {
                        done = YES;
                    }
     ];

    if(wait) {
        while (!done)
            [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
    }
}

Second, the superview of the subview that you want to transition is actually going to get the transition. So this means that you'll have to add viewA and viewB to a subview, add this subview to another view and then do the animation.

OTHER TIPS

Here is a solution without any hacks with runloop. Just use the first message to run a transition to another view, and the second message to return to original state:

- (void) forwardTransitionFromView: (UIView *) viewA toView: (UIView *) viewB
{
    NSAssert(viewA.superview, @"The 'from' view should be added to a view hierarchy before transition");
    NSAssert(!viewB.superview, @"The 'to' view should NOT be added to a view hierarchy before transition");
    CGSize viewASize = viewA.bounds.size;
    viewB.frame = CGRectMake(0.0, 0.0, viewASize.width, viewASize.height);
    [UIView transitionWithView:viewA
                      duration:1.0
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        [viewA addSubview:viewB];
                    }
                    completion:NULL];
}

- (void) backwardTransitionFromView: (UIView *) viewB toView: (UIView *) viewA
{
    NSAssert([viewB.superview isEqual:viewA], @"The 'from' view should be a subview of 'to' view");
    [UIView transitionWithView:viewA
                      duration:1.0
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^{
                        [viewB removeFromSuperview];
                    }
                    completion:NULL];
}

Why dont you flip both views in same direction?

 -(void) FlipView:(bool) fromRight {

 if(fromRight) {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view1 cache:YES];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view2 cache:YES];

    [UIView commitAnimations]; 
 }
 else {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view1 cache:YES];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:YES];

    [UIView commitAnimations]; 


}
 }

and call the method like below:

    [view1 setHidden:NO];
    [self FlipView:YES];
    [view2 setHidden:YES];

to reverse the animation:

    [view2 setHidden:NO];
    [self FlipView:NO];
    [view1 setHidden:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top