UITableview didSelectRowAtIndexPath cancels double gestures using UITapGestureRecognizer

I set up a UITableView using a double-touch UITapGestureRecognizer. But attempts to double-tap a cell by the user only start didSelectRowAtIndexPath twice. Are these two supposed to work together?

(I know that instead of the didSelectRowAtIndexPath built-in behavior, I could use a single click gesture recognizer, but there is a problem with this: the cell also has a button that I can no longer click when I add a single click gesture recognizer. In addition, I saw examples for SO users creating double-click functions on didSelectRowAtIndexPath, but isn't that too much hacking?)

+6
source share
4 answers

You can use the one-touch gesture recognizer instead of didSelectRowAtIndexPath , even if there is a button in the cell. You just need to check if the touch location is inside the UIButton or not in order to handle both cases. Hope this helps!

+1
source

More info on Chris's answer:

  • cancelsTouchesInView
  • delaysTouchesBegan
  • delaysTouchesEnded

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html

In my case, I had a problem adding 2 gestures to the UIImageView hard drives that were in the custom UITableViewCell . It so happened that didSelectRowAtIndexPath: was called when you tap / double-tap the UIImageView button. When I had only one click gesture, didSelectRowAtIndexPath: not called (and for me this was the correct behavior.

To prevent didSelectRowAtIndexPath: when using two tapping gestures (single / double tapping), I added this code to the first touch gesture (single):

 tapGesture.cancelsTouchesInView = YES; tapGesture.delaysTouchesBegan = YES; 

After this tap change / double-key UIImageView (on top of the user cell) did not didSelectRowAtIndexPath:

+10
source

It looks like I can get didSelectRowAtIndexPath and a double-tap gesture recognizer to blend well with delayaysTouchesBegan and overrides the TouchesInView properties of the gesture recognizer.

The other option described by @MSgambel seems to work equally well.

+5
source

I tested the delayTouchesBegan method with double labels, but I found that a single click received by the table is then delayed, which makes the table interaction less susceptible to the user and possibly annoying.

My solution is a bit pedestrian, but I use a timer to detect taps in the didSelectRowAtIndexPath method. I record the tap counter "1" for the first press, and if the user does not press again for 0.2 seconds, it displays the selected item. If the user knocked for 0.2 seconds to count the tap "2", then I show another element (action sheet). I reset the tap count every time.

This method uses more code, but provides a quick response from the interface, and the user does not need to know what is going on behind the scenes - just that the user interface is reacting.

+1
source

Source: https://habr.com/ru/post/897354/


All Articles