长按手势识别器上出现错误的UIButton标签 [英] Getting wrong UIButton tag on Long press gesture recognizer

查看:27
本文介绍了长按手势识别器上出现错误的UIButton标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有很多与此类似的问题,但是我已经尝试了所有问题,并且由于无法解决问题,我正在发布我的问题.首先,我尝试的问题是:

1):

手势识别器可对经过测试且符合 特定视图 以及该视图所有子视图的触摸进行操作.因此,它必须与该视图相关联.要建立该关联,您必须调用UIView方法addGestureRecognizer:.手势识别器不参与视图的响应者链.

这种情况下的解决方案是为每个需要识别的视图都配备一个手势识别器,并将它们链接到相同的委托选择器.

注意:这个问题(和我的回答)起源于3月20日的 NSChat聊天室,2013.决定在此处发布以供将来参考.

I am aware that there are already many questions asked similar to this, but I have tried all of them, and on failing to solve my issue I am posting my question. First the questions I tried are:

1)How to get button.tag via longPressGestureRecognizer?

2)UIButton Long Press Event

In my application I have 12 UIButtons in my xib. On the long press of UIButton I have this method getting called. Using gesture.view.tag property always gives me same tag(i.e) every time when I click on different UIButtons.

- (IBAction)longPress:(id)sender {

     UILongPressGestureRecognizer* gesture=(UILongPressGestureRecognizer*)sender;
     NSLog(@"Tag---> %d",gesture.view.tag);
  }

My xib looks something like this:

Update 1:

Before someone gets confused with xib, I must say that UIButtons are set to Custom type so they are invisible under UIImageView.

解决方案

It appears that a UIGestureRecognizer can be tracking more than one view, but it does not report that it is tracking more than one view. Thus, when you check the view property of a UIGestureRecognizer, it is set to the last view that the recognizer was added to.

From the docs:

A gesture recognizer operates on touches hit-tested to a specific view and all of that view’s subviews. It thus must be associated with that view. To make that association you must call the UIView method addGestureRecognizer:. A gesture recognizer does not participate in the view’s responder chain.

The solution in this scenario is to have a gesture recognizer for each view that needs recognizing, and have them linked to the same delegate selector.

Note: this question (and my answer) originated in the NSChat chat room, on March 20th, 2013. It was decided to post here for future reference.

这篇关于长按手势识别器上出现错误的UIButton标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆