UITapGestureRecognizer 操作未触发; userInteractionEnabled 已设置为 YES

发布于 2024-11-17 19:28:36 字数 998 浏览 4 评论 0原文

我的程序基本上是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

煞人兵器 2024-11-24 19:28:36

我遇到了同样的问题,后来发现我将相同的手势识别器实例分配给另一个视图。手势识别器只能与单个视图关联(您可以通过 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.

荭秂 2024-11-24 19:28:36

第一个想法:
将您的方法更改为:

-(void) openDeal:(UIGestureRecognizer *) recognizer{
    NSLog(@"%@",[NSString stringWithFormat:@"%d", recognizer.view.tag]);
}

First idea:
Change your method to:

-(void) openDeal:(UIGestureRecognizer *) recognizer{
    NSLog(@"%@",[NSString stringWithFormat:@"%d", recognizer.view.tag]);
}
别再吹冷风 2024-11-24 19:28:36

你的 while 方法看起来很奇怪?您确定它正在运行吗?

尝试按如下枚举子视图数组,看看是否有帮助......

NSArray *subviewsArray = grid.subviews;
for (id imageView in subviewsArray)
{
    if ([imageView isKindOfClass:[UIImageView class]])
    {
     // run your code here
    }
}

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...

NSArray *subviewsArray = grid.subviews;
for (id imageView in subviewsArray)
{
    if ([imageView isKindOfClass:[UIImageView class]])
    {
     // run your code here
    }
}
羁客 2024-11-24 19:28:36

我刚刚遇到了同样的问题。

首先,检查视图属性。如果addGestureRecognizer之后为null,则说明不起作用。

在我删除对委托属性的分配后,我的问题得到了解决。

所以代码看起来像:

{
    UITapGestureRecognizer *bmSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bmSingleTap:)];
    bmSingleTap.numberOfTapsRequired = 1;
    //bmSingleTap.delegate = self;
    [bookmarkTapArea addGestureRecognizer:bmSingleTap];
    NSLog(@"tap: %@ %i", bmSingleTap.view, bmSingleTap.enabled);
}

编辑:顺便说一句,后来我想重现这个 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:

{
    UITapGestureRecognizer *bmSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bmSingleTap:)];
    bmSingleTap.numberOfTapsRequired = 1;
    //bmSingleTap.delegate = self;
    [bookmarkTapArea addGestureRecognizer:bmSingleTap];
    NSLog(@"tap: %@ %i", bmSingleTap.view, bmSingleTap.enabled);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文