简体   繁体   English

iOS Swift 3-向前和向后旋转

[英]ios swift 3 - rotation forwards and backwards

i want to rotate an image in the forward in order to reach 180 degree and then to go back to the first situation (backward with 180 degree) 我想向前旋转图像以达到180度,然后回到第一种情况(向后旋转180度)

in fact i see an example on how to rotate an image in one way during one second but i could not understand what should i change in the code to make it rotate in the first 0.5 second - forward and in the next 0.5 second - backward 实际上,我看到了一个示例,该示例说明了如何在一秒钟内以一种方式旋转图像,但我不明白我应该在代码中进行哪些更改,以使其在前0.5秒(向前)和后0.5秒(向后)中旋转

here is the code : 这是代码:

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")}

All you need is rotation.autoreverses = true 您只需要rotation.autoreverses = true

You can write, 你可以写,

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")

I have increased animation duration to see it properly you can reduce it to whatever value you want :) 我增加了动画的播放时间,以便可以正确看到它,可以将其减小为所需的任何值:)

1 Degree = 0.0174533 Radian rotation takes value in radians, hence 180 degree = 180 * 0.0174533 1度= 0.0174533弧度旋转采用弧度值,因此180度= 180 * 0.0174533

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM