Pan, Tap, and Long Press recognizers work, but Swipe gestures do not

131 Views Asked by At

I have tested the following code, and only the selectors for the pan, tap, and long press recognizers activate. I have tried adding the recognizers inside the UIView subclass and adding the recognizers as parameters. My only guess would be that the pan, tap, and long press are somehow gobbling up all of the touches, but I would expect only one of them to work in that case. Since the same procedures are followed for all recognizers, I don't really understand what the problem would be. Thanks in advance for any help.

init(level: LevelView) {
    self.level = level
    left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
    right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
    up = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp))
    down = UISwipeGestureRecognizer(target: self, action: #selector(swipeDown))
    tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
    pan = UIPanGestureRecognizer(target: self, action: #selector(pan(_:)))
    long = UILongPressGestureRecognizer(target: self, action: #selector(long(_: )))
    long.minimumPressDuration = 0.3
    long.allowableMovement = 30
    left.direction = .left
    right.direction = .right
    up.direction = .up
    down.direction = .down
    for rec in [up, down, right, left, tap, long, pan] {
        rec!.cancelsTouchesInView = false
        level.addGestureRecognizer(rec!)
    }
}

Update: The Pan recognizer was immediately recognizing the touches and not passing them to the swipe recognizers. Since I wanted the swipe recognizers to take precedence, I added the condition that all of the swipe gestures fail before the pan recognizer could recognize. That worked. Thank you to Matt for the helpful tip.

0

There are 0 best solutions below