旋转手势识别UIRotationGestureRecognizer

旋转手势识别UIRotationGestureRecognizer

以下创建手势将会添加到gestureView上

//将view的背景颜色设置为白色
 self.view.backgroundColor = UIColor.white
        //创建一个UIView
 let gestureView = UIView(frame: CGRect(x: 0, y: 100, width: 
 308, height: 308))
 gestureView.backgroundColor = UIColor.green
 self.view.addSubview(gestureView)

创建旋转手势

let rotation = UIRotationGestureRecognizer(target: self, action: #selector(rotationAction))

把旋转手势添加到gestureView上

gestureView.addGestureRecognizer(rotation)

实现旋转手势关联方法rotationAction

//MARK:- 旋转手势关联方法
    func rotationAction(sender:UIRotationGestureRecognizer){
        //sender.rotation手势旋转的弧度
        sender.view?.transform = (sender.view?.transform.rotated(by: sender.rotation))!
        //将上次的弧度置为1
        sender.rotation = 0
    }

定义一个backRandomColor方法用来随机变换背景颜色

 func backRandomColor()->UIColor {
        //产生0~1的随机数
        let redView = Float(arc4random_uniform(256))/255.0
        let greenView = Float(arc4random_uniform(256))/255.0
        let blueView = Float(arc4random_uniform(256))/255.0
        //产生随机颜色
        let color = UIColor(red: CGFloat(redView), green: CGFloat(greenView), blue: CGFloat(blueView), alpha: 1.0)
        return color
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容