如何让UIButton只响应触摸而不响应拖动

发布于 2024-10-16 21:42:41 字数 520 浏览 2 评论 0原文

看一下这个方法:

[textButton addTarget:self action:@selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchDown];

当按钮被触摸时,它会调用 文章ModalWillAppear:。效果很好。问题是按钮在拖动时也会调用该操作。 (按钮很大,包含一个文本段落)

我放置了六个这样的按钮作为 UIScrollView 的子视图(带有 UIPageControl)。水平拖动 UIScrollView 时效果很好。但垂直拖动时它会弹出模态视图,因为当手指拖动按钮时,按钮将其视为触摸,并且触摸会调用 articleModalWillAppear:

如果您仍然不明白我的问题,请考虑《纽约时报》iPad 应用程序。您可以水平拖动页面。您可以点击文章描述进入完整文章视图。但是,当您在文章描述中垂直拖动时,什么也不会发生。如何实现这一目标?

Take a look at this method:

[textButton addTarget:self action:@selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchDown];

When the button is touched, it calls articleModalWillAppear:. That works well. The problem is the button also calls the action when it is dragged. (The button is big and contains a text paragraph)

I put six such buttons as subviews of UIScrollView (with UIPageControl). It works well when dragging UIScrollView horizontally. But it pops up modal views when dragging vertically because when a finger dragging across a button, the button considers it as a touch, and the touch calls articleModalWillAppear:.

If you still don't understand my problem, think about the New York Times iPad app. You can drag the pages horizontally. You can touch an article description to go to the full article view. But nothing happens when you drag vertically inside an article description. How to achieve this?

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

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

发布评论

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

评论(2

酒绊 2024-10-23 21:42:41

使用不同的事件,例如:

[textButton addTarget:self action:@selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchUpInside];

UIControlEventTouchUpInside 是这种情况下最常用的。

Use a different event e.g.:

[textButton addTarget:self action:@selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchUpInside];

UIControlEventTouchUpInside is the most generally used in this case.

与他有关 2024-10-23 21:42:41

使用 forControlEvents:UIControlEventTouchUpInside

仅当用户在按钮内按下并向上触摸同一按钮时才注册单击事件,并忽略拖动。

use forControlEvents:UIControlEventTouchUpInside

That only registers a click event when a user touches down inside the button and then touches up in the same button, and ignores drags.

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