翻转和缩放的3D过渡问题(的iOS 7+)(Flip and zoom 3D transition

2019-11-03 16:25发布

我试图建立卡翻转和缩放过渡。 我做了它的模型在折纸- 电影(小于1MB)

正如你可以看到有3个变换同时施加:1.移动视图的中心到屏幕2的中心的缩放3. 3D rotte

我们可以等于2个阶段划分的动画:
阶段1:按钮秤,移动和上侧(PI / 2弧度)旋转。 在阶段1个的结束按钮消失。
第2阶段: toView (我的模态的视图)旋转(从-PI / 2到0弧度),可扩展到正常大小和移动视图的中心到屏幕中心。 如果我们要排除反弹 - 这是非常简单的变换。

不过,同时建立它,我见过几个问题:
1.我翻转只是一个按钮,而不是整个视图。 有这个按钮后面的背景。 所以,如果我申请CATransform3DMakeRotation按钮的半消失-它具有3D空间(Z轴)的背景相交。 有没有在这里使用3DTransform的方法吗?
2.在屏幕上的一些文物。 我没有在那里他们来自任何想法。

您可以检查结果电影 (3.8MB)。 我放慢动画电影中的第二张牌。

这是我为这个动画代码:

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

let container = transitionContext.containerView()

let fromViewBig = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

// Converting button to UIImageView and hiding actual button
let button = sourceViewController.view.viewWithTag(sourceViewController.buttonIndex)!
UIGraphicsBeginImageContextWithOptions(button.bounds.size, false, 0.0)
button.layer.renderInContext(UIGraphicsGetCurrentContext())
var img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
let fromView = UIImageView(frame: button.frame)
fromView.image = img
button.hidden = true

container.addSubview(toView)
container.addSubview(fromView)



let duration = self.transitionDuration(transitionContext)

// Add a perspective transform
var transform = CATransform3DIdentity
transform.m34 = -0.002
container.layer.sublayerTransform = transform

//calculating "median" frame size - size of the frame in 1/2 of animation
let medianX = (toView.frame.width + fromView.frame.width) / 2
let medianY = (toView.frame.height + fromView.frame.height) / 2

// at the midpoint of our animation final size of fromView should be equal to starting size of toView
// initial scale transfor fo toView (it will appear in second half of animation)
let initialToScaleTransform = CATransform3DMakeScale(medianX/toView.frame.width, medianY/toView.frame.height, 1.0)
// final scale of fromView
let finalFromScaleTransform = CATransform3DMakeScale(medianX/fromView.frame.width, medianY/fromView.frame.height, 1.0)
let finalToScaleTransform = CATransform3DMakeScale(1.0, 1.0, 1.0)

// our view is moving diring animation too
let startCenter = fromView.center
let endCenter = toView.center
let midCenter = CGPoint(x: (startCenter.x + endCenter.x)/2, y: (startCenter.y + endCenter.y)/2)

// flip the to VC halfway round - hiding it
toView.layer.transform = CATransform3DRotate(initialToScaleTransform, CGFloat(-M_PI_2), 0.0, 1.0, 0.0)
toView.center = midCenter

// animate
UIView.animateKeyframesWithDuration(duration, delay: 0, options: UIViewKeyframeAnimationOptions.allZeros, animations: { () -> Void in
    UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.5, animations: { () -> Void in
        fromView.layer.transform = CATransform3DRotate(finalFromScaleTransform, CGFloat(M_PI_2), 0.0, 1.0, 0.0)
        fromView.center = midCenter
    })
    UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.5, animations: { () -> Void in
        toView.layer.transform = CATransform3DRotate(finalToScaleTransform, 0.0, 0.0, 1.0, 0.0)
        toView.center = endCenter
    })
}) { (finished) -> Void in
    transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
}
文章来源: Flip and zoom 3D transition problems (iOS 7+)