各种触摸手势介绍

1.轻拍手势

let tap = UITapGestureRecognizer()
        self.view.addGestureRecognizer(tap)
        tap.addTarget(self, action: #selector(topAction))
        //拍几下
        tap.numberOfTapsRequired=2
        //几个手指拍
        tap.numberOfTouchesRequired = 2
 func tapAction(){
        print("轻拍")
    }

2.长按手势

let long = UILongPressGestureRecognizer()
        self.view.addGestureRecognizer(long)
        long.addTarget(self, action: #selector(longAction))
        //长按事件 默认是0.5
        long.minimumPressDuration = 1
        //长按时可以挪动的最小距离,默认是10
        long.allowableMovement = 15
        //长按手指数量,次数同轻拍
  func longAction(){
         print("长按")
    }

3.轻扫

let swip = UISwipeGestureRecognizer()
        swip.direction = .left
        swip.addTarget(self, action: #selector(swipAction(swip:)))
        self.view.addGestureRecognizer(swip)
        let swip1 = UISwipeGestureRecognizer()
        swip.direction = .right
        swip.addTarget(self, action: #selector(swipAction(swip:)))
        self.view.addGestureRecognizer(swip1)
 func swipAction(swip:UISwipeGestureRecognizer){
        // 获取轻扫的方向
        let dir=swip.direction
        //print(dir)
        if dir == .left{
           
        }else if dir == .right {
            
        }
       // print("轻扫的方向\(dir)")
    }

4.旋转手势

let rota = UIRotationGestureRecognizer()
        ()
        self.view.addGestureRecognizer(rota)
        rota.addTarget(self,action:#selector(rotaAction))
        //旋转角度
        // rota.rotation
func rotaAction(){
        
    }

5.捏合手势

 let pin = UIPinchGestureRecognizer()
        pin.addTarget(self, action: #selector(pinAction))
        self.view.addGestureRecognizer(pin)
        //缩放比
        //pin.scale
  func pinAction(){
        print("捏合")
    }

6.拖动手势

 let pan = UIPanGestureRecognizer()
        pan.addTarget(self, action: #selector(panAction(pan:)))
        //pan.translation(in: <#T##UIView?#>)
        self.view.addGestureRecognizer(pan)
  func panAction(pan:UIPanGestureRecognizer){
        print(pan.translation(in: self.view))
    }
       

你可能感兴趣的:(各种触摸手势介绍)