gpt4 book ai didi

swift - CoreAnimation 和设备旋转

转载 作者:行者123 更新时间:2023-11-28 08:18:54 28 4
gpt4 key购买 nike

我有一个 CAKeyframeAnimation,它在风景中看起来很棒,但是当设备旋转时,与其余内容相比,动画当然不合适。有没有办法调整(缩放)正在进行的动画的路径?

编辑:添加代码

        let circle = CAShapeLayer()
circle.path = UIBezierPath(roundedRect: CGRect(x: -60.0, y: -60.0, width: 100.0, height: 100.0), cornerRadius: 50.0).cgPath
circle.fillColor = UIColor.clear.cgColor
circle.strokeColor = UIColor.blue.cgColor

let animation = CAKeyframeAnimation(keyPath: "position")
animation.duration = (path["endTime"] as! Double) - (path["startTime"] as! Double)
animation.repeatCount = 0
animation.path = (path as! UIBezierPath).cgPath
animation.calculationMode = kCAAnimationPaced
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false

circle.add(animation, forKey: "position")
self.view.layer.insertSublayer(circle, at: 5)

问题是 UIBezierPath 是在纵向或横向(应用程序支持两者)中定义的,因此以与创建时相同的方向播放是没有问题的,但是如果您旋转设备,则 UIBezierPath 定义的动画应该是针对新方向进行了更新。我只是在寻找一种在设置动画时将 CAKeyframeAnimation 路径更新到新坐标系的方法。

最佳答案

我发现您无法即时调整路径,但您可以根据原始路径中的点和计算出的比例/平移创建新路径。

要从 UIBezierPath 获取 CGPoints 数组,您可以使用此方法:https://stackoverflow.com/a/36374209/1491675

对于动画开始后的设备旋转,保留对动画中当前点的引用,并在设备旋转时重新计算路径,并用新动画替换现有动画,删除已经发生的动画部分。

关于swift - CoreAnimation 和设备旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41794961/

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