2010-07-01 79 views
4

當我輕敲姿勢火災,我需要與它一起發送額外的說法,但我必須做一些非常愚蠢的,我究竟錯在這裏做:柄敲擊手勢參數iPhone/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線的運行。

我需要知道什麼可識別的圖像被點擊的東西。

回答

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

至少我可以通過超過1個參數來阻止我的腦袋 - 謝謝! 但我仍然留在同一個地方,試圖找出一些東西來確定哪個圖像被點擊。我知道一個圖像已經被點擊了,因爲它是該視圖中響應水龍頭的唯一的東西,只是不知道哪一個。 – Slee 2010-07-01 18:57:32

+0

對不起點擊=點擊上面的 – Slee 2010-07-01 18:58:02

+0

有沒有辦法獲得視圖在超視圖中的位置的索引?假設我添加了40個UIImageView,並且發送的是28個 - 這對我來說完全適用。 – Slee 2010-07-01 19:07:45