如何在同一视图上交替使用平移手势和滑动手势?

时间:2015-11-10 18:43:05

标签: ios swift uigesturerecognizer uipangesturerecognizer uiswipegesturerecognizer

我有一个包含计数器的视图。

我已经实现了两种不同的手势识别器,

UISwipeGesture将计数增加一,

和UIPanGesture,以便每次增加多个数字

用户刷牙的时间。

两种手势都有效,但我的问题是它们不能同时工作。

我希望他们能够交替工作,例如,如果我做小滑动

计数器应该增加1,如果我继续向上拖动计数器

应增加多个数字。

这是我的代码的一部分:

```

override func viewDidLoad() {
    super.viewDidLoad()

    setupSwipeGestures()
    setupPanGestures()
} 

private func setupSwipeGestures() {
    let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

    swipeUp.direction = .Up
    swipeDown.direction = .Down

    circleView.addGestureRecognizer(swipeUp)
    circleView.addGestureRecognizer(swipeDown)
}


private func setupPanGestures() {
    let panGesture = UIPanGestureRecognizer(target: self, action: Selector("handleThePan:"))
    circleView.addGestureRecognizer(panGesture)
}



extension UIPanGestureRecognizer {
    func isDown(circleView: UIView) -> Bool {
       let velocity : CGPoint = velocityInView(circleView)
         if velocity.y < 0 {
            print("ex Gesture went up")
            return false
         } else {
            print("ex Gesture went down")
            return true
         }
     }
 }

```

P.S:方法“handleSwipe”和“handlePan”包含计数器的逻辑。

1 个答案:

答案 0 :(得分:11)

无法同时识别滑动和平移手势,因为滑动手势只是一种特殊类型的平移,必须更快,更短,并且在一个方向上。

您应该做的是设置平移手势以要求滑动手势在它们开始之前失败。您需要在UISwipeGestureRecognizer

上将每个requireGestureRecognizerToFail设置为UIPanGestureRecognizer
override func viewDidLoad() {
    super.viewDidLoad()

    let swipeGestures = setupSwipeGestures()
    setupPanGestures(swipeGestures: swipeGestures)
} 

private func setupSwipeGestures() -> [UISwipeGestureRecognizer] {
    let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

    swipeUp.direction = .Up
    swipeDown.direction = .Down

    circleView.addGestureRecognizer(swipeUp)
    circleView.addGestureRecognizer(swipeDown)

    return [swipeUp, swipeDown]
}

private func setupPanGestures(swipeGestures: [UISwipeGestureRecognizer]) {
    let panGesture = UIPanGestureRecognizer(target: self, action: Selector("handleThePan:"))
    for swipeGeature in swipeGestures {
        panGesture.requireGestureRecognizerToFail(swipeGesture)
    }
    circleView.addGestureRecognizer(panGesture)
}

Swift 3

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeGestures = setupSwipeGestures()
    setupPanGestures(swipeGestures: swipeGestures)
}

private func setupSwipeGestures() -> [UISwipeGestureRecognizer] {
    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipes))
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipes))

    swipeUp.direction = .up
    swipeDown.direction = .down

    circleView.addGestureRecognizer(swipeUp)
    circleView.addGestureRecognizer(swipeDown)

    return [swipeUp, swipeDown]
}

private func setupPanGestures(swipeGestures: [UISwipeGestureRecognizer]) {
    let panGesture = UIPanGestureRecognizer.init(target: self, action:#selector(self.handleThePanUp))
    for swipeGesture in swipeGestures {
        panGesture.require(toFail: swipeGesture)
    }
    circleView.addGestureRecognizer(panGesture)
}