UIButton 的触摸检测添加为 UIScrollView 中的子视图
我有一个带有许多按钮的滚动视图。我想检测滚动视图内 UIButton 的触摸,但无法获取它们。我尝试通过子类化 uiscrollview 和 uibutton 但失败了。如果用户按住该按钮很短时间(例如 2 秒),我想检测按钮上的触摸。然后我想将按钮拖动到用户可以在滚动视图上拖动它的位置。请帮助我。
I have a scroll view with many buttons. I want to detect the touches on UIButton inside scrollView but not been able to get them. I tried by subclassing uiscrollview, and uibutton
but failed. I want to detect touches on button if user holds that button for a short time, say 2 sec. And then i want to drag the button to place where user can drag it on scroll view. Plz help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不需要子类化。
尝试将
UILongPressGestureRecognizer
附加到您的UIButton
。请查看此处了解更多信息信息。
iOS 3.2 开始提供手势识别器,使所有与手势相关的事情变得非常简单。 在这里您可以找到教程。
如果您想支持以前的版本,则必须采用不同的方法:
向按钮添加
UIControlEventTouchUpInside
和UIControlEventTouchDown
操作;在 touchDown 处理程序中开始计时(用当前时间设置一个变量);
在 touchUp 处理程序中停止计时;测量差异,如果高于阈值,则触发操作。
如果这不起作用,请提供更多信息来说明为什么不起作用。
You don't need to subclass.
Try to attach to your
UIButton
aUILongPressGestureRecognizer
.Have a look here for more info.
Gesture recognizers are available from iOS 3.2 and make very easy all things related to gestures. Here you can find a tutorial.
If you want to support previous versions, you have to resort to a different method:
add
UIControlEventTouchUpInside
andUIControlEventTouchDown
actions to your button;in the touchDown handler start counting time (set a variable with current time);
in touchUp handler stop counting time; measure the difference and if is above your threshold, fire you action.
If this does not work, provide some more information as to why it doesn't, please.
您可以为
scrollView
上的每个按钮设置一个tag
,然后像 @sergio 所说的那样为每个按钮添加一个UILongPressGestureRecognizer
(或 uicontrolevent),因此,当您在滚动视图中设置页面时,您可以添加:或
并在您的操作中..
或
(显然在您的.h上添加UIGestureRecognizerDelegate)
You can set a
tag
for each button on thescrollView
, then like @sergio said add aUILongPressGestureRecognizer
(or a uicontrolevent) to each button, so when you are setting the pages in the scrollview you could add:or
and in your action..
or
(obviously add UIGestureRecognizerDelegate on your .h)