gpt4 book ai didi

ios - 如何使用 CGAffineTransform Identity 进行 View Controller 转换

转载 作者:行者123 更新时间:2023-11-29 02:10:13 25 4
gpt4 key购买 nike

我一直在尝试在呈现另一个 View Controller 时进行自定义转换,我以两种方式成功了。一种是通过使用 CGAfflineTransformIdentity,另一种是使用 CGAffineTransformMakeTranslation 推送 ViewController。

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

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

那么 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)
})
}

最佳答案

当我使用单位矩阵转换 View 时我的动画是如何发生的

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

这解释了为什么在您的动画 block 中您可以调用 toView.transform = CGAffineTransformIdentity或者 toView.transform = offScreenDown . CGAffineTransformMakeTranslation(0, 0)只是由零点翻译的恒等变换。

要证明恒等变换只是撤销初始变换,您可以将其连接到目标变换:

toView.transform = CGAffineTransformConcat(offScreenDown,CGAffineTransformIdentity)

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

The identity transform is a data transformation that copies the source data into the destination data without change (Wikipedia)

意思是在你的动画 block 中 offScreenDown == CGAffineTransformIdentity == CGAffineTransformConcat(offScreenDown,CGAffineTransformIdentity) == CGAffineTransformConcat(CGAffineTransformIdentity,CGAffineTransformIdentity)

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

"For custom presentation transitions we setup an intermediate view between the window and the windows rootViewController's view. This view is the containerView that you perform your animation within. Due to an implementation detail of auto-rotation on iOS, when the interface rotates we apply an affine transform to the windows rootViewController's view and modify its bounds accordingly. Because the containerView inherits its dimensions from the window instead of the root view controller's view, it is always in the portrait orientation."

关于ios - 如何使用 CGAffineTransform Identity 进行 View Controller 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29360635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com