0

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]];

    }
}
Joao
  • 619
  • 1
  • 5
  • 14

3 Answers3

0

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.

Rick van der Linde
  • 2,581
  • 1
  • 20
  • 22
  • I edited the code accordingly, but still getting the same results. Could you please let me know what i'm still doing wrong? – Joao Dec 18 '13 at 15:53
  • You also moved both views inside another subview? – Rick van der Linde Dec 18 '13 at 16:05
  • Yup, as in: [containerView addSubview:viewA]; [containerView addSubview:viewB]; – Joao Dec 18 '13 at 16:09
  • Where is the animation code? Inside the container? If no, try to move it into the container and then call the flip animation from the superview. – Rick van der Linde Dec 18 '13 at 16:13
  • What do you mean by "code inside the container"? The container, as above, is generated dynamically. How do i add code to the container?? – Joao Dec 18 '13 at 16:24
  • Can you upload your project somewhere? I'll take a quick look at it. – Rick van der Linde Dec 19 '13 at 08:27
  • It seems there is a lot of misinformation about this issue a bit everywhere around. Got the damn thing to work using the info @ http://phildow.net/2012/05/31/flip-an-image-in-uiimageview-using-uiview-transitionwithview/ – Joao Dec 19 '13 at 15:04
  • Rick, your code works for fadein / fadaout views, but i was aiming at a flip animation. – Joao Dec 19 '13 at 15:05
  • Weird, it should work with the flip animation... The solution in the link you provided is also not switching between views, but between images inside an image view. Oh well, at least you got your problem fixed. :-) – Rick van der Linde Dec 19 '13 at 16:24
0

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];
}
nalexn
  • 10,615
  • 6
  • 44
  • 48
-1

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];
Mavericks
  • 524
  • 3
  • 5