How to flip an individual UIView (without flipping the parent view)

前端 未结 6 510
一整个雨季
一整个雨季 2020-11-27 15:16

This is an iPad project where I have a UIView with several subViews, and I am trying to animate one of this UIViews using [UIView transitionFromView:toView:duration:options:

6条回答
  •  执笔经年
    2020-11-27 15:31

    I had problems getting this to work also. I used the code written by Floris, but although it worked for the first "flip" the next flip started to go weird where the buttons and labels on the frontside UI View lost there positions.

    I put the code below into place and it is working fine.

    Couple of things to note:

    1. panelView is a UIView control on the ViewController that has two other UIViews nested inside it (subviews). The first one is called frontView, second one is called backView. (see picture below)

    image of the view and subviews in IB

    1. I have a bool property called displayingFront on the class

    (in my .h)

    @property BOOL displayingFront;
    

    in my .m

    @synthesize displayingFront;
    

    in my viewDidLoad method

    self.displayingFront = YES;
    

    This is the code in the .m that i have rigged up to the two buttons if front and back UIViews...

    - (IBAction)flip:(id)sender
    {
        [UIView transitionWithView:self.panelView
                          duration:1.0
                           options:(displayingFront ? UIViewAnimationOptionTransitionFlipFromRight :
                                    UIViewAnimationOptionTransitionFlipFromLeft)
                        animations: ^{
                            if(displayingFront)
                            {
                                self.frontView.hidden = true;
                                self.backView.hidden = false;
                            }
                            else
                            {
                                self.frontView.hidden = false;
                                self.backView.hidden = true;
                            }
                        }
    
                        completion:^(BOOL finished) {
                            if (finished) {
                                displayingFront = !displayingFront;
                            }
                        }];
    }
    

提交回复
热议问题