捏合手勢識別UIPinchGestureRecognizer

捏合手勢識別UIPinchGestureRecognizer

以下創(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)建捏合手勢

let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction))

把捏合手勢添加到gestureView上

gestureView.addGestureRecognizer(pinch)

實現(xià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
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容