在 UILongPressGestureRecognizer 上,如何检测哪个对象生成了事件?
我有几个 UIButton 的视图。我已成功使用 UILongPressGestureRecognizer 实现,并使用以下内容作为选择器;
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(@"Long Press");
}
}
在这个方法中我需要知道的是哪个 UIButton 收到了长按,因为我需要做一些不同的事情,具体取决于哪个按钮收到了长按。
希望答案不是将长按发生的坐标映射到按钮边界的问题 - 宁愿不去那里。
有什么建议吗?
谢谢!
I have a view with several UIButtons. I have successfully implemented using UILongPressGestureRecognizer with the following as the selector;
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(@"Long Press");
}
}
What I need to know within this method is which UIButton received the longpress since I need to do something different, depending on which button received the longpress.
Hopefully the answer is not some issue of mapping the coordinates of where the longpress occured to the bounds of the buttons - would rather not go there.
Any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可以在
gesture.view
中找到。This is available in
gesture.view
.您是否将长按手势控制器添加到将 UIButtons 作为子视图的 UIView 中?如果是这样,@Magic Bullet Dave 的方法可能是正确的选择。
另一种方法是子类化 UIButton 并向每个 UIButton 添加一个 longTapGestureRecogniser。然后,您可以使用按钮来执行您喜欢的操作。例如,它可以将标识自己的消息发送到视图控制器。以下代码片段说明了子类的方法。
在你的视图控制器中你可能有这样的代码:
Are you adding the long tap gesture controller to the UIView that has the UIButtons as subviews? If so, something along the lines of @Magic Bullet Dave's approach is probably the way to go.
An alternative is to subclass UIButton and add to each UIButton a longTapGestureRecogniser. You can then get your button to do what you like. For example, it could send a message identifying itself to a view controller. The following snippet illustrates methods for the subclass.
In your view controller you might have code something like this:
如果您的视图包含多个子视图(如许多按钮),您可以确定点击的内容:
If your view contains multiple subViews (like lots of buttons) you can determine what was tapped: