Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handle tap gesture with an argument iphone / ipad

when my tap gesture fires I need to send an additional argument along with it but I must be doing something really silly, what am I doing wrong here:

Here is my gesture being created and added:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:itemSKU:)];
tapGesture.numberOfTapsRequired=1;
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];

[self.view addSubview:imageView];

Here is where I handle it:

-(void) handleTapGesture:(UITapGestureRecognizer *)sender withSKU: (NSString *) aSKU {
        NSLog(@"SKU%@\n", aSKU);
}

and this won't run because of the UITapGestureRecognizer init line.

I need to know something identifiable about what image was clicked.

like image 740
Slee Avatar asked Dec 16 '22 23:12

Slee


1 Answers

A gesture recognizer will only pass one argument into an action selector: itself. I assume you're trying to distinguish between taps on various image subviews of a main view? In that case, your best bet is to call -locationInView:, passing the superview, and then calling -hitTest:withEvent: on that view with the resulting CGPoint. In other words, something like this:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
...
- (void)imageTapped:(UITapGestureRecognizer *)sender
{
    UIView *theSuperview = self.view; // whatever view contains your image views
    CGPoint touchPointInSuperview = [sender locationInView:theSuperview];
    UIView *touchedView = [theSuperview hitTest:touchPointInSuperview withEvent:nil];
    if([touchedView isKindOfClass:[UIImageView class]])
    {
        // hooray, it's one of your image views! do something with it.
    }
}
like image 163
Noah Witherspoon Avatar answered Dec 19 '22 12:12

Noah Witherspoon