移动开发iOS学习笔记iOS学习开发

旋转手势识别UIRotationGestureRecognize

2016-11-24  本文已影响644人  七叶5

旋转手势识别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
    }
上一篇下一篇

猜你喜欢

热点阅读