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

时间:2010-07-01 18:11:17

标签: 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];

这是我处理它的地方:

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

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

我需要知道可以识别哪些图像被点击的内容。

1 个答案:

答案 0 :(得分: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.
    }
}