如何让UIButton只响应触摸而不响应拖动
看一下这个方法:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用不同的事件,例如:
UIControlEventTouchUpInside
是这种情况下最常用的。Use a different event e.g.:
UIControlEventTouchUpInside
is the most generally used in this case.使用
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.