开发者

handle tap gesture with an argument iphone / ipad

开发者 https://www.devze.com 2023-01-05 15:27 出处:网络
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:

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.


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

精彩评论

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