每次单击按钮时将 UIButton 旋转 90 度

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)