UITapGestureRecognizer 操作未触发; userInteractionEnabled 已设置为 YES
我的程序基本上是这样的:
UIViewController ->自定义 UIView -> [UIImageView 数组]
我的问题是我的识别器的操作方法从未被调用。我已经将 UIImageViews 的 userInteractionEnabled 属性设置为 YES。
在我的视图控制器的 viewDidLoad:
- (void)viewDidLoad
{
NSEnumerator *enumerator = [grid.subviews objectEnumerator];
UIImageView *view;
UITapGestureRecognizer *recognizer;
while((view = [enumerator nextObject])){
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openDeal:)];
view.userInteractionEnabled = YES;
recognizer.delegate = self;
recognizer.numberOfTapsRequired = 1;
[view addGestureRecognizer:recognizer];
NSLog(@"%d", (int)view.userInteractionEnabled);
[recognizer release];
}
[super viewDidLoad];
}
和 openDeal 上定义如下:
-(void) openDeal:(UITapGestureRecognizer *) recognizer{
NSLog(@"%@",[NSString stringWithFormat:@"%d", recognizer.view.tag]);
}
My program basically looks like this:
UIViewController -> Custom UIView -> [Array of UIImageView]
My problem is that my recognizer's action method is never called. I've already set the userInteractionEnabled attribute of my UIImageViews to YES.
On my View Controller's viewDidLoad:
- (void)viewDidLoad
{
NSEnumerator *enumerator = [grid.subviews objectEnumerator];
UIImageView *view;
UITapGestureRecognizer *recognizer;
while((view = [enumerator nextObject])){
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openDeal:)];
view.userInteractionEnabled = YES;
recognizer.delegate = self;
recognizer.numberOfTapsRequired = 1;
[view addGestureRecognizer:recognizer];
NSLog(@"%d", (int)view.userInteractionEnabled);
[recognizer release];
}
[super viewDidLoad];
}
and openDeal is defined as such:
-(void) openDeal:(UITapGestureRecognizer *) recognizer{
NSLog(@"%@",[NSString stringWithFormat:@"%d", recognizer.view.tag]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,后来发现我将相同的手势识别器实例分配给另一个视图。手势识别器只能与单个视图关联(您可以通过 UIGestureRecognizer 的 view 属性进行验证)。
确保您没有在代码的其他地方重复使用识别器。
I had the same issue and later found out that I was assigning the same gesture recognizer instance to another view. Gesture recognizers can be associated to a single view only (you can verify that through UIGestureRecognizer's view property).
Make sure you are not reusing your recognizers somewhere else in the code.
第一个想法:
将您的方法更改为:
First idea:
Change your method to:
你的 while 方法看起来很奇怪?您确定它正在运行吗?
尝试按如下枚举子视图数组,看看是否有帮助......
Your
while
method looks odd? Are you sure it is running at all?Try enumerating the subviews array as per below and see if it helps...
我刚刚遇到了同样的问题。
首先,检查视图属性。如果addGestureRecognizer之后为null,则说明不起作用。
在我删除对委托属性的分配后,我的问题得到了解决。
所以代码看起来像:
编辑:顺便说一句,后来我想重现这个 bmSingleTap.view==nil 效果但不能。也许,干净的重建就能解决问题。
I have just had the same issue.
At first, check the view property. If it is null after addGestureRecognizer, it did not work.
My problem was solved after I removed assignment to the delegate property.
So the code looks like:
Edit: By the way, later I wanted to reproduce this bmSingleTap.view==nil effect and could not. Probably, a clean rebuild would do the trick.