处理带有参数iphone/ipad的轻拍手势

Sle*_*lee 4 iphone objective-c ipad

当我的点击手势触发时,我需要发送一个额外的参数,但我必须做一些非常愚蠢的事情,我在这里做错了什么:

这是我创建和添加的手势:

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];
Run Code Online (Sandbox Code Playgroud)

这是我处理它的地方:

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

并且由于UITapGestureRecognizer init行而无法运行.

我需要知道可以识别哪些图像被点击的东西.

Noa*_*oon 12

手势识别器只会将一个参数传递给动作选择器:本身.我假设你试图区分主视图的各种图像子视图上的点击?在这种情况下,您最好的选择是调用-locationInView:,传递超级视图,然后使用结果调用-hitTest:withEvent:该视图CGPoint.换句话说,这样的事情:

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.
    }
}
Run Code Online (Sandbox Code Playgroud)