Ios CABasicAnimation-在暂停期间更改动画开始值
为了以360度旋转圆,我使用CABASICANIATION,如下所示:Ios CABasicAnimation-在暂停期间更改动画开始值,ios,geometry,layer,image-rotation,cabasicanimation,Ios,Geometry,Layer,Image Rotation,Cabasicanimation,为了以360度旋转圆,我使用CABASICANIATION,如下所示: func startRotate(from : Double) { let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z") rotation.fromValue = from rotation.toValue = NSNumber(value: M_PI * 2 - from) ro
func startRotate(from : Double) {
let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.fromValue = from
rotation.toValue = NSNumber(value: M_PI * 2 - from)
rotation.duration = 10
rotation.isCumulative = true
rotation.repeatCount = 1
self.cdImage.layer.add(rotation, forKey: "rotationAnimation")
}
cdImage视图第一次从值0开始
当动画在5秒后到达中间位置时,我想停止动画并为cdImage视图设置变换,例如,将视图返回20度并从新位置恢复
通过这种方式:
1.我调用stopRotate函数
func stopRotate(from : Double) {
self.cdImage.layer.removeAnimation(forKey: "rotationAnimation")
}
我设置了一个新的转换
self.cdImage.transform = CGAffineTransform(rotationAngle: imageAngle * CGFloat(M_PI) / 180);
使用新值调用startRotate
startRotateimageAngle*CGFloatM_PI/180
我假设动画将从新位置开始,并再次以360度旋转视图,但实际上动画是从0度开始循环的,如何从新位置恢复新动画?如果要从停止的位置继续,当停止动画时,您需要: 捕捉它旋转了多少,看看; 通过检查表示层确定飞行中的值; 相应地设置层的变换,使其在移除动画时保持不变;和 删除动画 因此: 然后,可以在重新启动动画时从该角度开始动画
var angle: CGFloat?
func stopRotate() {
let transform = cdImage.layer.presentation()!.transform
angle = atan2(transform.m12, transform.m11);
cdImage.layer.transform = transform
cdImage.layer.removeAnimation(forKey: "rotationAnimation")
}