UIView vertical flip animation

后端 未结 8 762
温柔的废话
温柔的废话 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条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 17:54

    UIViewAnimationOptionTransitionFlipFromTop is easy to use, but we can not create an interactive transition using UIViewAnimationOptionTransitionFlipFromTop. We need change layer’s transform to create an interactive transition.

    Just create a transform using CATransform3DMakeRotation is not enough, no light effect, no perspective. I write an sample to add these effect. You can change it to an interactive transition easily.

    Demo:

    Flip effect

    Sample code:

    CALayer *sideALayer = sideAView.layer;
    CALayer *sideBLayer = sideBView.layer;
    CALayer *containerLayer = containerView.layer;
    
    sideALayer.opacity = 1;
    sideBLayer.opacity = 0;
    sideBLayer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
    containerLayer.transform = CATransform3DIdentity;
    
    CATransform3D perspectiveTransform = CATransform3DIdentity;
    perspectiveTransform.m34 = -1.0 / containerViewWidth;
    [UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
    
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
            sideALayer.opacity = 0;
            containerLayer.transform = CATransform3DConcat(perspectiveTransform,CATransform3DMakeRotation(M_PI_2, 0, 1, 0));
        }];
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
            sideBLayer.opacity = 1;
            containerLayer.transform = CATransform3DConcat(perspectiveTransform, CATransform3DMakeRotation(M_PI, 0, 1, 0));
        }];
    } completion:nil];
    

    sideAView and sideBView are subviews of containerView.

    The containerView is set a black background.

提交回复
热议问题