2011-06-29 95 views

回答

11

使用UITapGestureRecognizer手勢檢測來自UIView繼承的任何視圖的觸摸。

如下使用.....

添加敲擊手勢UITapGestureRecognizermyImageView視圖(類型的UIImageView)。

UITapGestureRecognizer *myTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTapEvent:)]; 
myImageView.userInteractionEnabled = YES; 
[myImageView addGestureRecognizer:myTapGesture]; 
[myTapGesture release];  

實施gestureTapEvent:接收觸摸事件的方法。

-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture 
{ 

} 

更新:

與您連接使用的UIGestureRecognizerview財產手勢,你可以訪問視圖。

@property(nonatomic, readonly) UIView *view; 

所以你的gestureTapEvent方法應該像下面那樣。

-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture 
{ 
    UIImageView* myImageView = (UIImageView*)gesture.view ; 
} 
+0

但我怎麼知道在tap方法中點擊了哪個imageview? – yosifz8

+0

@ yosifz8:檢查更新後的答案... – Jhaliya

2

你有多種可能性爲:

  • 使用UIGestureRecognizer在您的ImageView添加UITapGestureRecognizer(我建議這個解決方案)
  • 子類的UIImageView覆蓋touchesBegan:withEvent:方法等
  • 在您的UIImageView中添加一個UIButton(類型自定義,以避免要繪製的按鈕的框架)

例子:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClicked:)]; 
[cell.yourImageView addGestureRecognizer:gestureRecognizer]; 
[gestureRecognizer release]; 

[編輯]

然後,當你實現你的imageViewClicked方法,您可以使用GestureRecognizer的view屬性來獲取抽頭ImageView的。從開始,例如,您可以:

  • 使用您的ImageView的標籤(如果您在tableView:cellForRowAtIndexPath:方法的影響的話)來檢索標籤,做任何你想做的事情(取決於它影響到什麼,例如你可能已經在tableView:cellForRowAtIndexPath:中設置了imageView.tag = indexPath.row,然後得到那個indexPath行)
  • 通過imageView的超級視圖直到UITableViewCell,然後請求它的indexPath將其取回並根據需要執行任何操作。

例子:

-(void)imageViewClicked:(UITapGestureRecognizer*)gestRecognizer 
{ 
    UIImageView* iv = (UIImageView*)gestRecognizer.view; 
    NSInteger tag = iv.tag; // then do what you want with this 

    // or get the cell by going up thru the superviews until you find it 
    UIView* cellView = iv; 
    while(cellView && ![cellView isKindOfClass:[UITableViewCell class]]) 
     cellView = cellView.superview; // go up until you find a cell 

    // Then get its indexPath 
    UITableViewCell* cell = (UITableViewCell*)cellView; 
    NSIndexPath* indexPath = [self.tableView indexPathForCell:cell]; 
} 
+0

但我怎麼知道在tap方法中點擊了哪個imageview? – yosifz8

+0

你必須跟蹤indexPath.row – Maulik

+1

UIGestureRecognizer的'view'屬性(作爲參數傳遞給你的'imageViewClicked:'選擇器)將允許你訪問UIImageView。然後,您可以使用imageView的標籤來跟蹤objet或indexPath與哪個模型相關聯(如果您在返回單元格時設置標籤)或獲取父UITableViewCell並索要其indexPath。 (我會更新我的答案給代碼) – AliSoftware

0

相反的UIImageView,你可以自定義需要的UIButton和相同的圖像分配到按鈕,你會得到這個按鈕的touchupInside事件。只需指定button.tag = indexpath.row,以便您可以獲取按下哪個單元格的按鈕。

-(IBAction)ButtonPressed:(id)sender{ 
    UIButton *btnSelected = sender; 
    switch(btnSelected.tag){ 
     case: 0 
     ///Cell 0's Button Pressed. 
     break; 
     ...... 
     ...... 
} 

希望得到這個幫助。

0

要添加的觸摸事件,以一個UIImageView使用以下格式在您的m

UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newTapMethod)]; 

[_imageButtonName setUserInteractionEnabled:YES]; 

[_imageButtonName addGestureRecognizer:newTap]; 

然後創建newTapMethod(或你的名字什麼都)

-(void)newTapMethod{ 
//... 
} 

希望幫助。