如何动画逆时针旋转

时间:2017-02-02 17:11:38

标签: ios swift

我有一个分段控件,我想设置一个UIView动画,当控制器的右侧轻敲时顺时针旋转,逆时针旋转左侧。我要旋转的代码是:

func rotateRight() {
    UIView.animate(withDuration: 0.5, animations: {
        self.profileImageView.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(M_PI)) / 180.0)
        self.profileImageView.transform = CGAffineTransform(rotationAngle: (0.0 * CGFloat(M_PI)) / 360.0)
    })

然后我使用此代码来处理更改,具体取决于所点击的段(编辑用于更新):

    if loginRegisterSegmentedControl.selectedSegmentIndex == 0 {
        self.loginRegisterButton.backgroundColor = UIColor.blue
        loginRegisterSegmentedControl.tintColor = UIColor.blue
        rotateLeft()
        // Roll to the left
    } else {
        self.loginRegisterButton.backgroundColor = UIColor.red
        loginRegisterSegmentedControl.tintColor = UIColor.red
        rotateRight()
        // Roll to the right
    }

所以基本上在if条件中我想调用rotateLeft()函数 - 我该怎么做?

编辑:这是我的完整动画代码,包括皮尔斯的建议:

// Rotation
var viewAngle: CGFloat = 0    // Right-side up to start
let π = CGFloat.pi   // Swift allows special characters hold alt+p to use this, it allows for cleaner code

func rotate(by angle: CGFloat) {

    self.viewAngle += angle

    UIView.animate(withDuration: 0.5, animations: {
        self.profileImageView.transform = CGAffineTransform(rotationAngle: self.viewAngle)
        self.view.layoutIfNeeded()
    })         

}

lazy var profileImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "TTTdude")
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill

    imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(rotate)))
    imageView.isUserInteractionEnabled = true

    return imageView
}()



lazy var loginRegisterSegmentedControl: UISegmentedControl = {
    let sc = UISegmentedControl(items: ["iOS", "Android"])
    sc.translatesAutoresizingMaskIntoConstraints = false
    sc.tintColor = UIColor.black
    sc.selectedSegmentIndex = 0
    sc.addTarget(self, action: #selector(handleLoginRegisterChange), for: .valueChanged)
    return sc
}()

func handleLoginRegisterChange() {

    let title = loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex)
    loginRegisterButton.setTitle(title, for: .normal)

    if loginRegisterSegmentedControl.selectedSegmentIndex == 0 {
        self.loginRegisterButton.backgroundColor = UIColor.blue
        loginRegisterSegmentedControl.tintColor = UIColor.blue
        rotate(by: -2*π)
        // Roll to the left
    } else {
        self.loginRegisterButton.backgroundColor = UIColor.red
        loginRegisterSegmentedControl.tintColor = UIColor.red
        rotate(by: 2*π)
        // Roll to the right
    }

4 个答案:

答案 0 :(得分:3)

只需按顺时针方向旋转它,但将其乘以负数

func rotateLeft() {
    UIView.animate(withDuration: 0.5, animations: {
        self.profileImageView.transform = CGAffineTransform(rotationAngle: ((180.0 * CGFloat(M_PI)) / 180.0) * -1)
        self.profileImageView.transform = CGAffineTransform(rotationAngle: ((0.0 * CGFloat(M_PI)) / 360.0) * -1)
        self.view.layoutIfNeeded()
    })
}

我还应该提一下,当你说(180.0 * CGFloat(π)) / 180时 - 你只是说π,因为180/180就是一个。

在我看来,你想要一次做两次不同的旋转?我有点困惑。我发现有一件事非常有帮助,如果您正在顺时针和逆时针进行多次旋转,则跟踪视图的轴方向。执行类似创建名为viewAngle

的属性的操作
var viewAngle: CGFloat = 0    // Right-side up to start
let π = CGFloat.pi   // Swift allows special characters hold alt+p to use this, it allows for cleaner code

func rotate(by angle: CGFloat) {

    for i in 0 ..< 4 {
        UIView.animate(withDuration: 0.125, delay: 0.125 * Double(i), options: .curveLinear, animations: {

            self.viewAngle += angle/4
            self.rotateView.transform = CGAffineTransform(rotationAngle: self.viewAngle)
            self.view.layoutIfNeeded()
        }, completion: nil)
    }        

}

如果你想以正角度旋转右通道(即π,π/ 2,π/ 4,2 *π),或者以左角通过负角度。

向右旋转180度:

rotate(by: π)

向左旋转180度:

rotate(by: -π)
编辑:正如你提到的那样,直到我为自己尝试时才看到,当你插入π或2π时它是负还是正,它只是顺时针旋转。为了解决这个问题,我必须让它以-π/ 4的增量旋转。所以要逆时针做一个全圆旋转我做了一个-π/ 4动画循环,如果这是有道理的。它有效,但我觉得应该有一个更简单的方法来做到这一点。我很感激其他任何人都在喋喋不休。

答案 1 :(得分:0)

(速记4)

它将逆时针旋转180度

 self.viewRoatate.transform = CGAffineTransform(rotationAngle:  CGFloat.pi / -2)
            self.viewRoatate.transform = CGAffineTransform(rotationAngle:  CGFloat.pi)
self.view.layoutIfNeeded()

答案 2 :(得分:0)

func rotate(button: UIButton, time: Double, overlap: Double, clockWise: Bool) {
var angles = [CGFloat(Double.pi), CGFloat(Double.pi) * 2]
if !clockWise {
    angles = [CGFloat(Double(270) * .pi/180),
              CGFloat(Double(180) * .pi/180),
              CGFloat(Double(90) * .pi/180),
              CGFloat(Double(0) * .pi/180)]
}
for i in 0..<angles.count {
    UIView.animate(withDuration: time, delay: time - overlap * Double(i), animations: {
        button.transform = CGAffineTransform(rotationAngle: angles[i])
    }, completion: nil)
}

答案 3 :(得分:-2)

  

所以没有办法让视图顺时针360旋转一个功能,然后逆时针360旋转另一个功能(我可以打电话取决于所选的段)?

当然有一种方法:

enter image description here

你可以用任何视图做到这一点;旋转标签只是一个例子。当然,你不需要两个按钮;我只是这样做,向您展示我是如何触发这两个功能的。