[英]How do you avoid janky animation when using CATransform3DRotate in iOS?
[英]iOS Swift - How to do 360 degree flip animation using CABasicAnimation and CATransform3DRotate with "m34" transform?
尽管我尽了最大的努力,但我无法弄清楚如何使用 CATransform3DRotate 从左到右进行 UIView 的全周期旋转 animation 。 请注意,我已经可以使用 CABasicAnimation 将其旋转 360 度,但没有 CATransform3DRotate。 但是,我想使用.m34变换来获得透视深度。 涉及 UIView.animate/transitions 的解决方案对我想要完成的工作没有帮助。 任何帮助深表感谢。
编辑:我在 Stackoverflow 和其他地方进行了广泛搜索,但没有找到任何描述如何使用 CATransform3DRotate 进行 360 度旋转的内容。 在那里寻求帮助,因为这是一个以前似乎没有回答过的问题。 谢谢!
这是将 UIView 旋转 180 度的代码。
var transform = CATransform3DIdentity
transform.m34 = 1.0 / -200.0
let animation = CABasicAnimation(keyPath: "transform")
animation.toValue = CATransform3DRotate(transform, CGFloat( Double.pi), 0, 1, 0)
animation.duration = 0.8
animation.beginTime = 0.0
animation.fillMode = .forwards
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
cardView.layer.add(animation, forKey: "360flip")
您当前的代码正在替换动画层的整个变换,这意味着您不能对其应用透视变换。 您还遇到了结尾 state 在数学上与开头 state 相同的问题,为了解决这个问题,您尝试将动画链接在一起,并使用填充模式,这使整个事情变得更加复杂。
首先,让我们处理转换。 要获得您想要设置图层变换的.m34
的透视图,您已经在执行以下操作:
var perspective = CATransform3DIdentity
perspective.m34 = 1 / -200
cardView.layer.transform = perspective
接下来,要仅触摸图层变换的旋转,您可以使用比transform
更精确的关键路径:
let rotate = CABasicAnimation(keyPath: "transform.rotation.y")
rotate.fromValue = 0
这将使层变换的 rest 保持不变。
最后,当 animation 的结尾与开头相同时,如何强制 animation 引擎注意到差异? 使用byValue
:
rotate.byValue = CGFloat.pi * 2
这会迫使图层绕远至 360 度,而不是停留在原来的位置的懒惰捷径。
最终的完整代码是:
var perspective = CATransform3DIdentity
perspective.m34 = 1 / -200
cardView.layer.transform = perspective
let rotate = CABasicAnimation(keyPath: "transform.rotation.y")
rotate.fromValue = 0
rotate.byValue = CGFloat.pi * 2
rotate.duration = 2
cardView.layer.add(rotate, forKey: nil)
给出这个结果:
简而言之,你需要两组动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.