CGAffineTransform:恢复旋转方向

6

我正在使用以下代码将UIButton旋转360度:

self.btn.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(M_PI)) / 180.0)
    self.btn.transform = CGAffineTransform(rotationAngle: (0.0 * CGFloat(M_PI)) / 180.0)

问题是它从左向右旋转,我想要反转方向。 我尝试使用 -M_PI 或 -180 和其他组合,但没有成功。 我做错了什么?

1个回答

18

您可以应用负变换来反转方向:

self.btn.transform = CGAffineTransform(rotationAngle: -0.999*CGFloat.pi)

使用以下方法撤消:

self.btn.transform = CGAffineTransform.identity

请记住,变换会改变视图而不是实际对象。您不必“撤消”它,只需替换即可。

以下是一些简单的工作示例代码,显示逆时针动画:

import UIKit
import PlaygroundSupport

let view:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = UIColor.red
PlaygroundPage.current.liveView = view

let button = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))

button.backgroundColor = UIColor.white
view.addSubview(button)

UIView.animateKeyframes(withDuration: 2.0, delay: 0.0, options: .repeat, animations: {
    let rotation = -0.999*CGFloat.pi
    button.transform = CGAffineTransform(rotationAngle: rotation)
    print(rotation)
}, completion: nil)
这是结果:

animation


它正常工作了,只是不得不更改-0.90 * CGFloat.pi,谢谢。 - i6x86

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接