2
votes

I'm using UIView transitionFromView to flip between two views. I'm currently doing the following in a UIView subclass:

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.png"]];

imgView.frame = CGRectMake(0, 0, 100, 100);

UIView *detailView = [[UIView alloc] initWithFrame:imgView.frame];
detailView.backgroundColor = [UIColor redColor];

detailView.frame = imgView.frame;

[self addSubview:imgView];

[UIView transitionFromView:imgView toView:detailView duration:1.0 
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:^(BOOL finished){

                }
 ]; 

This works as expected. The transition performs as it should. The issue is that I need the transition to take place in a subview of the view that the previous code is contained within. So, I put everything in a container and try to animate that:

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.png"]];

imgView.frame = CGRectMake(0, 0, 100, 100);

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
UIView *detailView = [[UIView alloc] initWithFrame:imgView.frame];
detailView.backgroundColor = [UIColor redColor];

detailView.frame = imgView.frame;

[container addSubview:imgView];

[self addSubview:container];

[UIView transitionFromView:imgView toView:detailView duration:1.0 
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:^(BOOL finished){

                }
 ]; 

But in this case, the animation doesn't run. Instead, I only see the final result without the transition. Can anyone help me figure out how the two cases differ?

The same problem is described here, but I don't feel the existing 'solution' is sufficient to fully describe this behavior. Transition behavior using transitionFromView and transitionWithView

2

2 Answers

1
votes

Consider delaying the animation to another run loop, using dispatch_after and performSelector:withObject:afterDelay:

or use [CATransaction flush] after you add the subviews

More info at http://andrewmarinov.com/working-with-uiviews-transition-animations/

0
votes

I don't know why the first one works, but this is not the correct way to use this method. The view being transitioned away from will be removed from the hierarchy and the view being transitioned to will be added as part of the animation. So skip adding detailView (or use the UIViewAnimationOptionShowHideTransitionViews option.