1

I want to trigger the tap gesture whenever I tap on an item in my UIPickerView.

I'm using the following code for LongPress Gesture which works fine. However if I switch to UITapGestureRecognizer, nothing is triggered.

@IBOutlet weak var showClaims: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()
    showClaims.isUserInteractionEnabled = true
    let tapped = UITapGestureRecognizer(target: self, action: #selector(ClaimVC.SelectClaimInfo))
      
    showClaims.addGestureRecognizer(tapped)
}

func SelectClaimInfo() {
    GetClaimInfo()
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Nassif Bousaba
  • 386
  • 1
  • 4
  • 22
  • I'm not seeing where you added a tap gesture recognizer to showClaims. –  Feb 07 '17 at 15:32
  • Is .userInteractionEnabled true on the UIPickerView – Emptyless Feb 07 '17 at 15:46
  • @dfd I have replaced the tap with a longPress. Same code is used for tap as the one for long press. – Nassif Bousaba Feb 07 '17 at 16:28
  • @Emptyless yes it is enabled – Nassif Bousaba Feb 07 '17 at 16:28
  • You should set delegate to gesture recognizer so `shouldRecognizeSimultaneouslyWithGestureRecognizer` will be called – Zell B. Feb 07 '17 at 16:28
  • I thought so - you probably should show the *full* code that *isn't* working instead of what works. –  Feb 07 '17 at 16:38
  • @dfd code edited. – Nassif Bousaba Feb 07 '17 at 16:41
  • By asking for full code I wanted to see a bit more - where the "let" (shouldn't be called Tap but tap instead BTW) and the add reside, along with other things the may conflict. More to the point - why are you using delegates and `shouldRecognizeSimultaneouslyWithGestureRecognizer`? When I use three simultaneous gesture (tap, pan, rotate) things work without this. (I believe if I added a fourth - long press - this would be true too.) I'm hoping the full code would help me to answer the issue. –  Feb 07 '17 at 16:47
  • @dfd I was trying something with long press and tap, then added the simultaneous gesture function but turns out it has no use. Ill update the full code now – Nassif Bousaba Feb 08 '17 at 15:08
  • I'd think @Darkwonder is right, but there's a key difference going on. That code adds the gesture to the view while your code adds it to the UIPickerView. If that's the issue (I'm not sure) you *could* wire up the tap to the superview and use a *hitTest()* on the UIPickerView's layer. –  Feb 08 '17 at 15:28
  • @dfd I've tried the same code on a label field and it works. Probably it is something with the picker view that is missing – Nassif Bousaba Feb 08 '17 at 15:38
  • I'm not sure what your need is, but if it's the picker view maybe you could do a hitTest() on it's layer? –  Feb 08 '17 at 16:15

2 Answers2

1

For who Tap Gesture not work for them until make everything as that:

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

try to conforms UIGestureRecognizerDelegate and then mKE tap.delegate = self and you must implement this UIGestureRecognizerDelegate method make

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Hosny
  • 821
  • 1
  • 13
  • 25
0

I believe @dfd is correct in the comments.

Example:

override func viewDidLoad()
{
    super.viewDidLoad()
    setupTapGesture()
}

private func setupTapGesture()
{
    let tapGesture = UITapGestureRecognizer(target: self,
                                            action: #selector(myVC.doStuff(_:)))
    view.addGestureRecognizer(tapGesture)
}

func doStuff(gesture: UITapGestureRecognizer) {}

You did miss the:

view.addGestureRecognizer(tapGesture)

Update: check touchesBegan from this SO: touchesBegan Swift 3.0 With it you can check if your UIPicker is reaciving touchers properly.

Community
  • 1
  • 1
Darkwonder
  • 1,149
  • 1
  • 13
  • 26