2012-07-20 55 views

回答

45

這實際上比您想象的要容易。您只需確保您在imageView上啓用用戶交互,並且您可以爲其添加輕擊手勢。這應該在單元被實例化時完成,以避免將多個輕敲手勢添加到相同的圖像視圖。例如:

- (instancetype)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self = [super initWithCoder:aDecoder]) { 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapMethod:)]; 

     [self.imageView addGestureRecognizer:tap]; 
     [self.imageView setUserInteractionEnabled:YES]; 
    } 

    return self; 
} 

- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture 
{ 
    UIImageView *imageView = (UIImageView *)tapGesture.view; 
    NSLog(@"%@", imageView); 
} 
+0

在myTapMethod中,你將如何訪問imageView? – oyalhi 2015-12-21 18:27:00

+1

@oyalhi UIGestureRecognizer類有一個[view](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/index.html#//apple_ref/occ/instp/UIGestureRecognizer/view)財產,將讓你在那裏。看到我更新的答案。 – 2015-12-21 19:39:03

3

可以使用的CustomButton代替的UIImageView

+0

謝謝,簡單的解決方法。 – dzensik 2016-01-11 16:39:59

0

敲擊手勢識別器可以非常記憶餓了,可以在頁面上導致其他的東西打破。我個人會重新建議你創建一個自定義表格單元格,在自定義單元格框架中插入一個按鈕,並在tableview代碼中將self.customtablecell.background.image設置爲你想要的圖像。通過這種方式,您可以將按鈕分配給IBaction,以便將其推送到您想要的任何視圖。

6

試試這個

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused) 
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)]; 
tap.cancelsTouchesInView = YES; 
tap.numberOfTapsRequired = 1; 
[imageView addGestureRecognizer:tap]; 

// handle method 
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer 
{ 
    RKLogDebug(@"imaged tab"); 
} 

確保u有....

imageView.userInteractionEnabled = YES; 
+0

+ 1表示將識別器傳遞到函數中。爲什麼這個「tap.delegate = self」;當你沒有真正使用它? – Kevin 2014-01-06 11:57:19

0

使用ALActionBlocks在塊動作

__weak ALViewController *wSelf = self; 
imageView.userInteractionEnabled = YES; 
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) { 
    NSLog(@"pan %@", NSStringFromCGPoint([weakGR locationInView:wSelf.view])); 
}]; 
[self.imageView addGestureRecognizer:gr];