Custom transition between two UIViews

后端 未结 6 607
暗喜
暗喜 2020-12-17 01:55

I have two views \"A\" and \"B\". A floats in the middle of the window (it\'s not full screen). B, the view which will replace A, is full screen. I\'d like to write a custom

6条回答
  •  佛祖请我去吃肉
    2020-12-17 02:51

    I've done this before. Here's the gist of how I did it:

    1. View A is on screen
    2. Create View B, give it a frame that makes it fullscreen, but don't add it to the screen yet
    3. View B has a UIImageView as it's topmost view
    4. Capture a UIImage of View A, and set it as the image of View B's image view

      UIGraphicsBeginImageContext(view.bounds.size); 
      [viewA.layer renderInContext:UIGraphicsGetCurrentContext()]; 
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      viewB.imageView.image = image;
      
    5. Set View B's transform (scale and translation) so that it is shrunk to the size of View A and is positioned where View A is on the screen

    6. Add View B to the screen
    7. At this point, the user hasn't noticed anything change, because View B looks exactly like View A
    8. Now start an animation block, in which you
      • Set [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:ViewB
      • set View B's transform to CGAffineTransformIdentity
      • remove View B's imageview from View B

    The result is an animation that looks like View A is flipping over and zooming to fill the screen with View B as the opposite side.

提交回复
热议问题