旋轉(zhuǎn)手勢識別UIRotationGestureRecognizer
以下創(chuàng)建手勢將會添加到gestureView上
//將view的背景顏色設(shè)置為白色
self.view.backgroundColor = UIColor.white
//創(chuàng)建一個UIView
let gestureView = UIView(frame: CGRect(x: 0, y: 100, width:
308, height: 308))
gestureView.backgroundColor = UIColor.green
self.view.addSubview(gestureView)
創(chuàng)建旋轉(zhuǎn)手勢
let rotation = UIRotationGestureRecognizer(target: self, action: #selector(rotationAction))
把旋轉(zhuǎn)手勢添加到gestureView上
gestureView.addGestureRecognizer(rotation)
實現(xiàn)旋轉(zhuǎn)手勢關(guān)聯(lián)方法rotationAction
//MARK:- 旋轉(zhuǎn)手勢關(guān)聯(lián)方法
func rotationAction(sender:UIRotationGestureRecognizer){
//sender.rotation手勢旋轉(zhuǎn)的弧度
sender.view?.transform = (sender.view?.transform.rotated(by: sender.rotation))!
//將上次的弧度置為1
sender.rotation = 0
}
定義一個backRandomColor方法用來隨機變換背景顏色
func backRandomColor()->UIColor {
//產(chǎn)生0~1的隨機數(shù)
let redView = Float(arc4random_uniform(256))/255.0
let greenView = Float(arc4random_uniform(256))/255.0
let blueView = Float(arc4random_uniform(256))/255.0
//產(chǎn)生隨機顏色
let color = UIColor(red: CGFloat(redView), green: CGFloat(greenView), blue: CGFloat(blueView), alpha: 1.0)
return color
}