每次单击按钮时将 UIButton 旋转 90 度 [英] Rotating a UIButton by 90 degrees every time the button is clicked

查看:20
本文介绍了每次单击按钮时将 UIButton 旋转 90 度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在每次单击按钮时将 UIButton 旋转 90 度并跟踪每个旋转的位置/角度?

How do you rotate a UIButton by 90 degrees each time the button is clicked and also keep track of each rotated position/angle?

这是我到目前为止的代码,但它只旋转一次:

Here is the code I have so far but it only rotates once:

@IBAction func gameButton(sender: AnyObject) {
    UIView.animateWithDuration(0.05, animations: ({
        self.gameButtonLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
    }))
}

推荐答案

self.gameButtonLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

应该改为

// Swift 3 - Rotate the current transform by 90 degrees.
self.gameButtonLabel.transform = self.gameButtonLabel.transform.rotated(by: CGFloat(M_PI_2))

// OR

// Swift 2.2+ - Pass the current transform into the method so it will rotate it an extra 90 degrees.
self.gameButtonLabel.transform = CGAffineTransformRotate(self.gameButtonLabel.transform, CGFloat(M_PI_2))

使用 CGAffineTransformMake...,您可以创建一个全新的转换并覆盖按钮上已有的任何转换.由于您想将 90 度附加到已经存在的变换(可能已经旋转了 0、90 等度),因此您需要添加到当前变换.我给出的第二行代码就可以做到这一点.

With CGAffineTransformMake..., you create a brand new transform and overwrite any transform that was already on the button. Since you want to append 90 degrees to the transform that already exists (which could be 0, 90, etc degrees rotated already), you need to add to the current transform. The second line of code I gave will do that.

这篇关于每次单击按钮时将 UIButton 旋转 90 度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆