Custom transition between two UIViews

后端 未结 6 609
暗喜
暗喜 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:49

    It's a little rough around the edges, but here's what I did using block animations. The following flips the view, centers it and goes fullscreen:

    // the view on the other side of the card
    UIView* redView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, viewToFlip.frame.size.width, viewToFlip.frame.size.height)];
    redView.backgroundColor = [UIColor redColor];
    
    // add a green square to the red view so we have something else to look at
    UIView* greenSquare = [[UIView alloc]initWithFrame:CGRectMake(10.0, 10.0, 20.0, 20.0)];
    greenSquare.backgroundColor = [UIColor greenColor];    
    [redView addSubview:greenSquare];
    
    // the flip animation
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:UIViewAnimationCurveLinear
                     animations:^{
    
                         // rotate 90 degrees
                         [viewToFlip.layer setTransform: CATransform3DRotate(viewToFlip.layer.transform, -M_PI/2, 0.0, 1.0, 0.0)];
    
                     } 
                     completion:^(BOOL finished){
    
                         // now that we're 90 degrees, swap one view for the other
                         // but if we add it now, the view will be backwards, so flip the original view back
                         [viewToFlip.layer setTransform: CATransform3DRotate(viewToFlip.layer.transform, M_PI, 0.0, 1.0, 0.0)];
                         [viewToFlip addSubview:redView];
    
                         // now let's continue our rotation
                         [UIView animateWithDuration:0.25
                                               delay:0.0
                                             options:UIViewAnimationCurveLinear
                                          animations:^{
    
                                              // rotate the last 90 degrees
                                              [viewToFlip.layer setTransform: CATransform3DRotate(viewToFlip.layer.transform, -M_PI/2, 0.0, 1.0, 0.0)];
    
                                          } 
                                          completion:^(BOOL finished){
    
                                              [UIView animateWithDuration:0.25
                                                                    delay:0.0
                                                                  options:UIViewAnimationCurveLinear
                                                               animations:^{
    
                                                                   // animate the views to the center of the screen and adjust their bounds to fill up the screen
                                                                   [viewToFlip setCenter:CGPointMake(self.view.center.x, self.view.center.y)];
                                                                   [viewToFlip setBounds:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
                                                                   [redView setCenter:CGPointMake(self.view.center.x, self.view.center.y)];
                                                                   [redView setBounds:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
                                                               }
                                                               completion:nil];
    
    
                                          }];
    
                     }];
    

提交回复
热议问题