手势

作者: 峰远 | 来源:发表于2016-12-14 14:53 被阅读0次
    点击
    let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(tap))
    tapGesture.numberOfTapsRequired = 1 // 点击次数
    tapGesture.numberOfTouchesRequired = 2   // 手指数
    self.view.addGestureRecognizer(tapGesture)
    
    func tap() {
        print("tap")
    }
    
    长按
    let longPressGesture = UILongPressGestureRecognizer.init(target: self, action: #selector(longPress))
    longPressGesture.minimumPressDuration = 1 // 长按时间
    longPressGesture.allowableMovement = 10 // 手势识别允许最大10像素移动
    self.view.addGestureRecognizer(longPressGesture)
    
    func longPress(sender:UIGestureRecognizer) {
        let longPressGesture = sender as! UILongPressGestureRecognizer
        if longPressGesture.state == .began { // 开始
            print("begin")
        }else if longPressGesture.state == .changed { // 改变
            print("change")
        }else if longPressGesture.state == .ended { // 结束
            print("end")
        }else if longPressGesture.state == .cancelled { // 取消
            print("cancel")
        }else if longPressGesture.state == .failed { // 失败
            print("fail")
        }
    }
    
    轻扫
    let swipeGesture = UISwipeGestureRecognizer.init(target: self, action: #selector(swipe))
    swipeGesture.direction = .left // 手势方向
    self.view.addGestureRecognizer(swipeGesture)
    
    func swipe(sender:UISwipeGestureRecognizer) {
        if sender.direction == .left { // 向左轻扫
            print("left")
        }else if sender.direction == .right { // 向右轻扫
            print("right")
        }else if sender.direction == .up { // 向上轻扫
            print("up")
        }else if sender.direction == .down { // 向下轻扫
            print("down")
        }
    }
    
    捏合
    let pinchGesture = UIPinchGestureRecognizer.init(target: self, action: #selector(pinch))
    self.view.addGestureRecognizer(pinchGesture)
    
    func pinch(sender:UIPinchGestureRecognizer) {
        if sender.state == .changed {
            print(sender.scale)
        }else if sender.state == .ended {
        
        }
    }
    
    拖动
    let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(pan))
    self.view.addGestureRecognizer(panGesture)
    
    func pan(sender:UIPanGestureRecognizer) {
        let point = sender.translation(in: self.view)
        print("\(point)")
    }
    
    旋转
    let rotationGesture = UIRotationGestureRecognizer.init(target: self, action: #selector(rotation))
    self.view.addGestureRecognizer(rotationGesture)
    
    func rotation(sender:UIRotationGestureRecognizer) {
        if sender.state == .changed {
            print("\(sender.rotation)")
        }
    }
    

    相关文章

      网友评论

          本文标题:手势

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