用参数iphone/ipad处理点击手势

用参数iphone/ipad处理点击手势,iphone,objective-c,ipad,Iphone,Objective C,Ipad,当我的点击手势触发时,我需要发送一个附加的参数,但我必须做一些非常愚蠢的事情,我做错了什么: 下面是我正在创建和添加的手势: UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:itemSKU:)]; tapGesture.numberOfTapsRequired=1; [imageView setUse

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

下面是我正在创建和添加的手势:

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);
}
由于UITapGestureRecognitizer init行的原因,此操作无法运行


我需要知道有关单击图像的可识别信息。

手势识别器只会将一个参数传递给动作选择器:自身。我假设您试图区分主视图的不同图像子视图上的点击?在这种情况下,最好的方法是调用
-locationInView:
,传递superview,然后在该视图上调用
-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.
    }
}

至少我可以停止在一次以上的争论中碰头——谢谢!但我仍然停留在同一个地方,试图找出一些东西来识别点击了哪个图像。我知道图像已经被点击了,因为它是该视图中唯一响应点击的对象,只是不知道是哪一个。对不起,点击=在上面点击有没有办法获取视图在superview中的位置的索引?假设我添加了40个UIImageView,发送的是28个,这对我来说非常合适。您可以在创建每个视图时将其“tag”属性设置为一个不同的数字,然后在上述方法中检索标记,并根据SKUs.DOH-sweet数组对其进行索引!我看到我可以将标签设置为int,只是没有点击我的脑袋-谢谢!