Transition behavior using transitionFromView and t

2019-01-13 21:51发布

I am attempting to create a transition between two subviews (view1 and view2). When a button is pressed I want view1 (front) to flip and show view2 (back). I have tried both transitionFromView and transitionWithView. Each works - but each has a problem.

transitionFromView - flips the superview (the whole window view flips, not the subviews). When this flip happens - one subview is on the front of the superview before the flip, and the other subview is on the back of the flip - as it should be. But I don't want the superview to flip, just the subviews.

transitionWithView - flips only the subviews - but the 'to' view gets displayed before the transition happens.

Anyone have a suggestion?

-(IBAction) button1action:(id) sender {  

 if ([sender tag] == 0) {  

  [UIView transitionFromView:view2 toView:view1 duration:2.0   
  options:UIViewAnimationOptionTransitionFlipFromLeft   
  completion:nil];   
}  

 else {  
  [view1 removeFromSuperview];    
  [self.view addSubview:view2];  
  [UIView transitionWithView:view2   
    duration:2.0  
    options:UIViewAnimationOptionTransitionFlipFromRight +  
    UIViewAnimationOptionShowHideTransitionViews  
      animations:^{}   
     completion:nil];  
 }
}

7条回答
ら.Afraid
2楼-- · 2019-01-13 22:20

I have run into this exact same problem, and I agree with the earlier answer. It seems you must have a container view for animating a transition from one subview to another subview.

I'm using the transitionFromView approach. To have a background behind the animation, you will also need a superview with the background for the container view.

My code looks like:

    [UIView transitionFromView:view1
                        toView:view2
                      duration:0.5
                       options:UIViewAnimationOptionTransitionFlipFromRight |
                               UIViewAnimationOptionAllowUserInteraction    |
                               UIViewAnimationOptionBeginFromCurrentState
                    completion:nil];
查看更多
登录 后发表回答