Search code examples
iosswiftuiviewuigesturerecognizeruitapgesturerecognizer

UIPanGesture is Working but UITapGesture Is not Working on UIView


I have a small view that I want to be moved around the screen and tapable. So I add a UIPanGestureRecognizer to move it, and a UITapGestureRecognizer to receive tap events like so:

        let panner = UIPanGestureRecognizer(target: self, action: #selector(panDidFire(panner:)))
        playerViewController.view.addGestureRecognizer(panner)

        let tapper = UITapGestureRecognizer(target: self, action: #selector(viewTapped(tapper:)))
        playerViewController.view.addGestureRecognizer(tapper)

And I create the actions in the same file

    func viewTapped(tapper: UITapGestureRecognizer){
         fadeInButtons()
    }
    func panDidFire(panner: UIPanGestureRecognizer) {
        let offset = panner.translation(in: view)
        panner.setTranslation(CGPoint.zero, in: view)
        var center = playerViewController.view.center
        center.x += offset.x
        center.y += offset.y
        playerViewController.view.center = center
    }

The panDidFire() function is called when the user pans, but the viewTapped() function is not called at all.

Is there a trick to this? What am I doing wrong? Is there another way of going about this?

I am using Xcode 8 with swift 3


Solution

  • You have to set to your recognizers to work with other gesture recognizer on the view. Please use method of UIGestureRecognizerDelegate

    gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
    {
       return true
    }