0

我一直在尝试在呈现另一个视图控制器时进行自定义转换,并且我以两种方式成功。一种是通过使用 CGAfflineTransformIdentity,另一种是通过使用 CGAffineTransformMakeTranslation 推送 ViewController。

Apple 文档将 CGAfflineTransformIdentity 描述为单位单位矩阵。当我用单位矩阵转换视图时,我的动画是如何发生的?

在实际数学中,当我将一些东西与单位矩阵相乘时,我得到了相同的矩阵。

那么 CGAfflineTransformIdentity 是如何真正实现转换的呢?

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

    let offScreenUp = CGAffineTransformMakeTranslation(0, -container.frame.size.height )
    let offScreenDown = CGAffineTransformMakeTranslation(0, 0)

    toView.transform = offScreenUp

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

    let duration = self.transitionDuration(transitionContext)       
    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.8, options: nil, animations: {
          toView.transform = CGAffineTransformIdentity
         //toView.transform = offScreenDown
        }, completion: { finished in
         // tell our transitionContext object that we've finished animating
         transitionContext.completeTransition(true)
    })
}
4

1 回答 1

1

当我用单位矩阵转换视图时,我的动画是如何发生的

答案是从技术上讲,用单位矩阵转换视图没有任何作用。但是,将视图的转换设置为单位矩阵将撤消该视图上的任何现有转换。因此,如果它已被缩放或旋转,您将有效地撤消该转换。在您的情况下,这意味着您正在撤消将视图向上移动的平移变换,因此现在它会回到原点。

这解释了为什么在你的动画块中你可以调用toView.transform = CGAffineTransformIdentityOR toView.transform = offScreenDownCGAffineTransformMakeTranslation(0, 0)只是零点转换的恒等变换。

为了证明身份转换只是撤消初始转换,您可以将其连接到目标转换:

toView.transform = CGAffineTransformConcat(offScreenDown,CGAffineTransformIdentity)

正如预期的那样,这没有效果,因为恒等变换仅在计算中复制变换的内容:

身份转换是一种数据转换,它将源数据复制到目标数据中而不进行更改(维基百科)

这意味着在您的动画块内offScreenDown== CGAffineTransformIdentity== CGAffineTransformConcat(offScreenDown,CGAffineTransformIdentity)==CGAffineTransformConcat(CGAffineTransformIdentity,CGAffineTransformIdentity)

在 iOS 7 上,重要的是要注意身份转换,因为它与动画转换有关,因为我发现容器视图对其子视图应用了转换以确保它们处于正确的方向(因为容器实际上位于新的 UIWindow 实例中)。这实际上是在开发者论坛中提到的:

“对于自定义演示转换,我们在窗口和窗口 rootViewController 的视图之间设置了一个中间视图。这个视图是您在其中执行动画的 containerView。由于 iOS 上自动旋转的实现细节,当界面旋转时,我们应用仿射变换到窗口 rootViewController 的视图并相应地修改其边界。因为 containerView 从窗口而不是根视图控制器的视图继承其尺寸,所以它始终处于纵向。

于 2015-04-02T08:11:37.307 回答