2012-07-20 20 views
24

में UIImageView के लिए पहचान टैप करें वर्तमान में मुझे एक समस्या का सामना करना पड़ रहा है, जब मैं अपने UITableViewCell पर UIImageView टैप किया गया था, तो मैं कार्रवाई करना चाहता हूं।UITableViewCell

प्रश्न: मैं यह कैसे कर सकता हूं? क्या कोई मुझे कोड, या कोई ट्यूटोरियल दिखा सकता है?

अग्रिम धन्यवाद!

+0

को UITapGestureRecognizer जोड़ें करने के लिए अपने UIImageView। – robertvojta

उत्तर

45

यह आपके विचार से वास्तव में आसान है। आपको बस यह सुनिश्चित करने की ज़रूरत है कि आप छवि दृश्य पर उपयोगकर्ता इंटरैक्शन सक्षम करें, और आप इसमें एक टैप इशारा जोड़ सकते हैं। यह तब किया जाना चाहिए जब सेल को उसी छवि दृश्य में जोड़े गए एकाधिक टैप जेस्चर से बचने के लिए तत्काल किया जाता है। उदाहरण के लिए:

- (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 में, आप छवि दृश्य को कैसे एक्सेस करेंगे? – oyalhi

+1

@oyalhi UIGestureRecognizer क्लास में एक [व्यू] है (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/index.html#//apple_ref/occ/instp/UIGestureRecognizer/view) संपत्ति जो आपको वहां ले जाएगी। मेरा अद्यतन उत्तर देखें। फ़ंक्शन में पहचानकर्ता को पारित करने के लिए –

3

आप एक customButton बजाय UIImageView

+0

धन्यवाद, सरल कामकाज। – dzensik

0

एक टैप जेस्चर recogniser का उपयोग बहुत स्मृति भूखा हो सकता है कर सकते हैं और तोड़ने के लिए पृष्ठ पर अन्य बातों के हो सकता है। मैं व्यक्तिगत रूप से आपको एक कस्टम टेबल सेल बनाने के लिए पुनर्निर्मित करता हूं, कस्टम सेल फ्रेम में एक बटन डालें और टेबलव्यू कोड में self.customtablecell.background.image को अपनी इच्छित छवि पर सेट करें। इस तरह आप जो भी दृश्य चाहते हैं उसे धक्का देने के लिए बटन को एक आईबैक्शन असाइन कर सकते हैं।

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"); 
} 

यकीन है कि यू कर प्रयास करें ....

imageView.userInteractionEnabled = YES; 
+0

+ 1। यह क्यों "tap.delegate = self;" जब आप वास्तव में इसका उपयोग नहीं कर रहे हैं? – Kevin

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];