1

I have a view (I'll call it parentView) that has about 20-30 subviews. I have added a long press gesture recognizer to the parentView. The gesture recognizer only seems to fire when I press on the parentView, it does not fire when I press and hold on one of the subviews.

I have tried adding the gesture recognizer to self.view and using the gesture location to see if it was within the bounds of the parentView. However the same problem occurs since it does not seem to detect my long press on the subviews.I have also tried running a for loop and adding the gesture recognizer to each individual subview but this also did not work.

This is how I am defining my gesture recognizer if anybody was wondering.

longPress.minimumPressDuration = 1
longPress.addTarget(self, action: #selector(ViewController.handleLongPress)
parentView.addGestureRecognizer(longPress)

How would I get the long press gesture recognizer to detect a long press on the parentView and it's subviews?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
coder
  • 381
  • 2
  • 22
  • You might want to take a look here: https://stackoverflow.com/a/15814834/2331445 – Stephan Schlecht Jan 05 '19 at 19:15
  • I looked at that question and tried the solution but it still **did not work.** I added the `UIGestureRecognizerDelegate`, added the line `longPress.delegate = self` and added the method but just put `return true` instead of the `if statement` in the original answer. – coder Jan 05 '19 at 19:38
  • You should return false for your subviews, as in the linked answer, otherwise it will not work (because the subviews are consuming the events). In Swift you would use this: https://developer.apple.com/documentation/uikit/uiview/1622521-isdescendant to determine whether the receiver is a descendant subview of the given view. – Stephan Schlecht Jan 05 '19 at 19:52
  • One naive way is to add one new gesture for each subview. This can give you 20-30 new gestures. It should work. – E.Coms Jan 05 '19 at 20:13
  • @E.Coms I just tried this and it does not seem to work. I don't know if this issue is something too do with the gesture or my subviews (I have made sure `.isUserInteractionEnabled` is set to `true`). – coder Jan 05 '19 at 21:39
  • So the view hierarchy has problems somewhere. – E.Coms Jan 05 '19 at 21:47

0 Answers0