Rotating a UIButton by 90 degrees every time the button is clicked(每次单击按钮时将 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 度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:每次单击按钮时将 UIButton 旋转 90 度


基础教程推荐
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- iOS4 创建后台定时器 2022-01-01