开发者

UITapGestureRecognizer action not being triggered; userInteractionEnabled already set to YES

开发者 https://www.devze.com 2023-03-16 07:36 出处:网络
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 userInterac

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:r开发者_如何学编程ecognizer];
        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]);
}


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:

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消