2

When I am tapping on UIView but Gesture is not working.

override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
        viewForSHadow.isUserInteractionEnabled = true
        viewForSHadow.addGestureRecognizer(tap)

        // Do any additional setup after loading the view.
    }


func handleTap(_sender: UITapGestureRecognizer) {
        print("---------View Tapped--------")
        viewForSHadow.isHidden = true
        viewForAlert.isHidden = true
    }

I just want to perform this action on UIView tap.

Coding
  • 85
  • 1
  • 7

6 Answers6

3

You may check in the debug view hierarchy if anything with alpha = 0 is overlapping your viewForSHadow.

R. Shah
  • 56
  • 4
1

You have to implement as follows:

    class ViewController: UIViewController, UIGestureRecognizerDelegate {

...

    override func viewDidLoad() {
        super.viewDidLoad()
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_sender:)))
        viewForSHadow.isUserInteractionEnabled = true
        viewForSHadow.addGestureRecognizer(tap)
    }

    @objc func handleTap(_sender: UITapGestureRecognizer) {
        print("---------View Tapped--------")
        viewForSHadow.isHidden = true
        viewForAlert.isHidden = true
    }
...

}
Bhavik Modi
  • 1,517
  • 14
  • 29
1

Your code is more than enough to have working UIGestureRecognizer, you should check some other stuff like, is there something else that can consume the user interaction. And also to check if you use

isUserInteractionEnabled = false

to some parent view of viewForSHadow.

m1sh0
  • 2,236
  • 1
  • 16
  • 21
1

You have to use UIGestureRecognizerDelegate

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDismiss))
        tapRecognizer.delegate = self
        blackView.addGestureRecognizer(tapRecognizer)

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return true
}

@objc func handleDismiss() {
        print("handleDismiss")
}

Reference

Don't forget to set the UIGestureRecognizerDelegate in your class

0

You need to mention numberOfTapsRequired property of the UITapGestureRecognizer.

override func viewDidLoad() {
    super.viewDidLoad()
    let tap = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_sender:)))
    tap.numberOfTapsRequired = 1
    viewForSHadow.isUserInteractionEnabled = true
    viewForSHadow.addGestureRecognizer(tap)
}

@objc func handleTapGesture(_sender: UITapGestureRecognizer) {
    print("---------View Tapped--------")
    // Why are you hiding this view **viewForSHadow**
    viewForSHadow.isHidden = true
    viewForAlert.isHidden = true
}

Also, make sure you are not doing viewForSHadow.isUserInteractionEnabled = false anywhere in the code.

  • sorry, not agree with you, numberOfTapRequired have default value 1, this is the reason why this can't be the problem. https://developer.apple.com/documentation/uikit/uitapgesturerecognizer/1623581-numberoftapsrequired – m1sh0 May 31 '19 at 10:02
  • Can you check if you are not doing ```viewForSHadow.isUserInteractionEnabled = false``` anywhere? – Ankush Bhatia May 31 '19 at 10:11
0

First of all you code doesn't compile. The handleTap(_:) signature must be like:

@objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("---------View Tapped--------")
}

Secondly, you need to first try with the minimal code in a separate project. And by minimal code I mean what you've added in the question.

class ViewController: UIViewController {
    @IBOutlet weak var viewForSHadow: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
        viewForSHadow.addGestureRecognizer(tap)
    }

    @objc func handleTap(_ sender: UITapGestureRecognizer) {
        print("---------View Tapped--------")
    }
}

Try with just the above code and see if you can get it working. The above code is working well at my end without any delegate or numberOfTaps.

PGDev
  • 23,751
  • 6
  • 34
  • 88