每次单击button时将UIButton旋转90度

每次单击button时,如何将UIButton旋转90度,并跟踪每个旋转的位置/angular度?

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

@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... ,你创build一个全新的变换,并覆盖已经在button上的任何变换。 既然你想追加90度到已经存在的变换(可能是已经旋转的0度,90度等),你需要添加到当前的变换中。 我给出的第二行代码将做到这一点。