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

查看:117
本文介绍了每次单击按钮时,将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天全站免登陆