Swift-手势

作者: 文艺小年青 | 来源:发表于2016-12-14 20:15 被阅读0次

    Swift 多种手势

    • 一,轻拍
    //轻拍
            let tap = UITapGestureRecognizer()
            self.view.addGestureRecognizer(tap)
            tap.addTarget(self, action: #selector(tapAction))
            //拍几下,
            tap.numberOfTapsRequired = 2
            //几个手指拍
            tap.numberOfTouchesRequired = 2
    
    • 二,长按
    //长按
            let long = UILongPressGestureRecognizer()
            self.view.addGestureRecognizer(long)
            long.addTarget(self, action: #selector(longAction))
            //长安的时间  默认是0.5
            long.minimumPressDuration = 1
            //长按时可以挪动的最小距离  默认是10
            long.allowableMovement = 15
            //长按的手指数,次数同轻拍
    
    • 三,轻扫
    //轻扫
            let swip = UISwipeGestureRecognizer()
            swip.direction = .left
            swip.addTarget(self, action: #selector(swipAction(swip:)))
            self.view.addGestureRecognizer(swip)
            let swip1 =    UISwipeGestureRecognizer()
            swip1.direction = .right
            swip1.addTarget(self, action: #selector(swipAction(swip:)))
            self.view.addGestureRecognizer(swip1)
    
    • 四,旋转
    //旋转
            let rota = UIRotationGestureRecognizer()
            self.view.addGestureRecognizer(rota)
            rota.addTarget(self, action: #selector(rotaAction))
            //旋转的角度  rota.rotation
    
    • 五,捏合,拖动
    //捏合
            let pin = UIPinchGestureRecognizer()
            pin.addTarget(self, action: #selector(pinAction))
            self.view.addGestureRecognizer(pin)
            //缩放比
            //pin.scale
            
            //拖动
            let pan = UIPanGestureRecognizer()
            pan.addTarget(self, action: #selector(panAction(pan:)))
            self.view.addGestureRecognizer(pan)
    
    • 各种手势的方法
    //拖动的方法
        func panAction (pan:UIPanGestureRecognizer) {
            //返回当前拖动的位置在self.view上的位置
    //        print(pan.translation(in: self.view))
            print(pan.translation(in: self.view))
            
        }
        //捏合的方法
        func pinAction() {
            print("捏合")
        }
        //旋转的方法
        func rotaAction() {
            print("旋转")
        }
        //轻扫的方法
        func swipAction(swip:UISwipeGestureRecognizer) {
            //获取轻扫的方向
            let dir = swip.direction
            if dir == .left {
                
            }else if dir == .right {
                
            }
            
    }
        //长按得方法
        func longAction() {
            print("长按")
        }
        //轻拍的方法
        func tapAction() {
            print("轻拍")
        }
    

    相关文章

      网友评论

        本文标题:Swift-手势

        本文链接:https://www.haomeiwen.com/subject/swqwmttx.html