UIView vertical flip animation

后端 未结 8 759
温柔的废话
温柔的废话 2020-11-30 17:09

I have an iOS UIView with UIViewAnimationTransitionFlipFromRight. I need it to flip vertically though. The page curl transition won\'t cut it. I assume this wil

8条回答
  •  -上瘾入骨i
    2020-11-30 17:50

    Just write your own method for the flip using Core Animation Transforms.

    - (void)verticalFlip{
        [UIView animateWithDuration:someDuration delay:someDelay animations:^{
            yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0);
        } completion:^{
            // code to be executed when flip is completed
        }];
    }
    

    Make sure you have the Core Animation libraries/frameworks added and included and also have included math.h if you want to use the M_PI notation.

    EDIT:

    To have it essentially "change" the view when it's halfway flipped do something like this:

    - (void)verticalFlip{
        [UIView animateWithDuration:someDuration delay:someDelay animations:^{
            yourView.layer.transform = CATransform3DMakeRotation(M_PI_2,1.0,0.0,0.0); //flip halfway
        } completion:^{
            while ([yourView.subviews count] > 0)
                [[yourView.subviews lastObject] removeFromSuperview]; // remove all subviews
            // Add your new views here 
            [UIView animateWithDuration:someDuration delay:someDelay animations:^{
                yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0); //finish the flip
            } completion:^{
                // Flip completion code here
            }];
        }];
    }
    

    This could also work:

    - (void)verticalFlip{
    
        // Do the first half of the flip
        [UIView animateWithDuration:someDuration delay:someDelay animations:^{
            yourView.layer.transform = CATransform3DMakeRotation(M_PI_2,1.0,0.0,0.0); //flip halfway
        } completion:^{
            while ([yourView.subviews count] > 0)
                [[yourView.subviews lastObject] removeFromSuperview]; // remove all subviews
            // Add your new views here 
        }];
    
        // After a delay equal to the duration+delay of the first half of the flip, complete the flip
        [UIView animateWithDuration:someDuration delay:durationOfFirstAnimation animations:^{
            yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0); //finish the flip
        } completion:^{
            // Flip completion code here
        }];
    }
    

    Cheers.

提交回复
热议问题