CoreAnimation 和设备旋转
CoreAnimation and Device Rotation
我有一个 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 路径更新到新坐标系的方法。
我发现您无法即时调整路径,但您可以根据原始路径中的点和计算出的点创建新路径 scale/translations。
要从 UIBezierPath 获取 CGPoints 数组,您可以使用此方法:
对于动画开始后的设备旋转,保留对动画中当前点的引用,并在设备旋转时重新计算路径并用新动画替换现有动画,省去动画中已经出现的部分.
我有一个 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 路径更新到新坐标系的方法。
我发现您无法即时调整路径,但您可以根据原始路径中的点和计算出的点创建新路径 scale/translations。
要从 UIBezierPath 获取 CGPoints 数组,您可以使用此方法:
对于动画开始后的设备旋转,保留对动画中当前点的引用,并在设备旋转时重新计算路径并用新动画替换现有动画,省去动画中已经出现的部分.