Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios swift 3-前后旋转_Ios_Swift_Rotation - Fatal编程技术网

ios swift 3-前后旋转

ios swift 3-前后旋转,ios,swift,rotation,Ios,Swift,Rotation,我想向前旋转图像,以达到180度,然后返回到第一种情况(向后旋转180度) 事实上,我看到了一个关于如何在一秒钟内以一种方式旋转图像的示例,但我不明白应该在代码中做哪些更改,以使其在前0.5秒(向前)和后0.5秒(向后)旋转 代码如下: func addRotation(forLayer layer : CALayer) { let rotation : CABasicAnimation = CABasicAnimation(keyPath:"transform.rotation.z")

我想向前旋转图像,以达到180度,然后返回到第一种情况(向后旋转180度)

事实上,我看到了一个关于如何在一秒钟内以一种方式旋转图像的示例,但我不明白应该在代码中做哪些更改,以使其在前0.5秒(向前)和后0.5秒(向后)旋转

代码如下:

func addRotation(forLayer layer : CALayer) {
    let rotation : CABasicAnimation = CABasicAnimation(keyPath:"transform.rotation.z")

    rotation.duration = 1.0
    rotation.isRemovedOnCompletion = false
    rotation.repeatCount = HUGE
    rotation.fillMode = kCAFillModeForwards
    rotation.fromValue = NSNumber(value: 0.0)
    rotation.toValue = NSNumber(value: 3.14 * 1.0)
         layer.add(rotation, forKey: "rotate")}

您只需旋转
rotation.autoreverses=true

你可以写

rotation.duration = 5.0
rotation.isRemovedOnCompletion = false
rotation.repeatCount = HUGE
rotation.fromValue = NSNumber(value: 0.0)
rotation.toValue = NSNumber(value: 180 * 0.0174533)
rotation.autoreverses = true
layer.add(rotation, forKey: "rotate")
我增加了动画的持续时间以正确地查看它。您可以将其减少到您想要的任何值:)

1度=0.0174533弧度 旋转以弧度为单位取值,因此 180度=180*0.0174533