UITableview didSelectRowAtIndexPath 通过 UITapGestureRecognizer 取代双击手势

发布于 2024-12-04 18:53:54 字数 261 浏览 3 评论 0原文

我已经设置了一个带有双击 UITapGestureRecognizer 的 UITableView。但是用户尝试双击单元格只会启动 didSelectRowAtIndexPath 两次。这两个人应该一起工作吗?

(我知道我可以使用单击手势识别器来代替 didSelectRowAtIndexPath 的内置行为,但这存在问题:单元格还有一个按钮,当我添加单击时我无法再按该按钮另外,我见过用户在 didSelectRowAtIndexPath 中构建双击功能的示例,但这不是有点太多了吗?)

I've set up a UITableView with a double-tap UITapGestureRecognizer. But attempts to double-tap a cell by the user just launches didSelectRowAtIndexPath twice. Are these two supposed to work together?

(i'm aware i could use a single tap gesture recognizer in place of the built-in behavior of didSelectRowAtIndexPath, but here's the problem with that: the cell also has a button that I can't press anymore when i add the single tap gesture recognizer. Also, I've seen examples on SO of users building double tap functionality into didSelectRowAtIndexPath, but isn't that a bit too much of a hack?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栩栩如生 2024-12-11 18:53:55

看起来我可以使用手势识别器的delaysTouchesBegan 和cancelsTouchesInView 属性让didSelectRowAtIndexPath 和双击手势识别器很好地协同工作。

@MSgambel 描述的另一个选项似乎同样有效。

Looks like I can get didSelectRowAtIndexPath and the double tap gesture recognizer to play together nicely using the delaysTouchesBegan and cancelsTouchesInView properties of the gesture recognizer.

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

心凉怎暖 2024-12-11 18:53:55

可以使用单击手势识别器来代替didSelectRowAtIndexPath,即使界面中有一个按钮细胞。您只需检查触摸位置是否位于 UIButton 视图内即可处理这两种情况。希望有帮助!

You can use a single-tap gesture recognizer in place of didSelectRowAtIndexPath, even if there is a button in the cell. You just need to check if the touch location is inside the UIButton's view or not in order to handle both cases. Hope that Helps!

暮倦 2024-12-11 18:53:55

我用双击测试了“delayTouchesBegan”方法,但我发现表格接收到的单击会被延迟,使得表格交互对用户的响应较差,并且可能很烦人。

我的解决方案有点乏味,但我使用计时器来检测方法 didSelectRowAtIndexPath 中的点击。我记录第一次点击的点击次数“1”,如果用户在 0.2 秒内没有再次点击,则会显示所选的项目。如果用户在 0.2 秒内点击了点击计数“2”,则我会显示另一个项目(操作表)。我每次都会重置点击计数。

此方法使用更多代码,但提供了界面的快速响应,并且用户不需要知道幕后发生了什么 - 只需 UI 具有响应能力即可。

I tested the "delayTouchesBegan" method with double taps but I found that the single tap received by the table is then delayed, making the table interaction less responsive to the user, and perhaps annoying.

My solution is a bit pedestrian but I use a timer to detect taps in method didSelectRowAtIndexPath. I record the tap count of "1" for the first tap, and if the user does not tap again with 0.2 seconds it displays the item selected. If the user tapped within 0.2 seconds for tap count "2" then I display another item (an action sheet). I reset the tap count each time.

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

野侃 2024-12-11 18:53:54

有关克里斯回答的更多信息:

  • cancelsTouchesInViewdelaysTouchesBegandelaysTouchesEnded
  • < a
  • href

="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html" rel="noreferrer">http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html

在我的例子中,我在添加 2 个点击手势时遇到了问题到自定义 UITableViewCell 上的 UIImageView。发生的情况是,当您点击/双击 UIImageView 时,会调用 didSelectRowAtIndexPath: 。当我只有一个点击手势时,不会调用 didSelectRowAtIndexPath: (对我来说,这是正确的行为。

为了防止在使用两个手势时调用 didSelectRowAtIndexPath:点击手势(单击/双击),我将此代码添加到第一个点击手势(单个):

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

此更改后,在 UIImageView 上点击/双击(在自定义单元格的顶部)做过不触发 didSelectRowAtIndexPath:

More info about Kris' 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 when adding 2 tap gestures to a UIImageView that was on a custom UITableViewCell. What happened was the didSelectRowAtIndexPath: was called when you tap/double tap on the UIImageView. When I had only one tap gesture, the didSelectRowAtIndexPath: was not called (and for me, that was the right behavior.

To prevent the didSelectRowAtIndexPath: from being called when using two tap gestures (single/double tap), I added this code to the first tap gesture (the single one):

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

After this change, a tap/double on the UIImageView (on top of the custom cell) did not trigger the didSelectRowAtIndexPath:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文