2011-03-30 13 views
5

मैं एक साधारण UIView के लिए एक इशारा पहचानकर्ता बनाने के लिए triyng हूँ:UITapGestureRecognizer एक UIView में जोड़ा नहीं काम करता है?

UIView *theView = [[UIView alloc] initWithFrame:rect]; 
[theView setUserInteractionEnabled:YES]; 

UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(handleTap)] autorelease]; 
[theView addGestureRecognizer:tap]; 

अगर मैं देखने की संपत्ति gestureRecognizers डिबग यह इशारा पहचानकर्ता वस्तु को दर्शाता है। लेकिन जब मैं दृश्य के अंदर टैप करता हूं तो यह काम नहीं करता है।

UIImageView का उपयोग कर एक ही कोड सही काम करता है, कोई विचार UIView में क्यों काम नहीं करता है?

अद्यतन:

एक उदाहरण वर्ग।

@implementation ExampleClass 

- (UIView *)getViewInRect:(CGRect)rect 
{ 
    UIView *theView = [[UIView alloc] initWithRect:rect]; 
    [theView setUserInteractionEnabled:YES]; 

    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(handleTap)] 
            autorelease]; 
    [aText addGestureRecognizer:tap]; 

    return theView; 
} 

- (UIImageView *)getImageViewInRect:(CGRect)rect 
{ 
    UIImageView *theView = [[UIImageView alloc] initWithRect:rect]; 
    [theView setUserInteractionEnabled:YES]; 

    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
             initWithTarget:self 
               action:@selector(handleTap)] 
            autorelease]; 
    [theView addGestureRecognizer:tap]; 

    return [theView autorelease];  
} 

- (void)handleTap 
{ 
    NSLog(@"Tap Handled!!", nil); 
} 

@end 

अपडेट 2:

theView के सभी subviews को UITapGestureRecognizer जोड़ा जा रहा है इस समस्या को ठीक नहीं है ...

इसे ठीक !!!

ठीक है !! समस्या के लिए CGRect समस्या थी, इसकी चौड़ाई 0.0 पर सेट थी !!!

+0

आपका हैंडलटैप फ़ंक्शन कैसा दिख रहा है? क्या आप व्यू के सुपरव्यू में एक इशारा रिकॉग्नाइज़र का उपयोग कर रहे हैं, जो इशारा पकड़ सकता है? – Seega

+0

हैंडलटैप एक ही कक्षा का एक तरीका है जहां उपर्युक्त कोड रहता है। इस स्कूप से मैं इसे [स्वयं हैंडलटैप] के साथ समस्याओं के बिना कॉल कर सकता हूं। –

+1

क्या आप कह रहे हैं कि यदि आपने उपरोक्त कोड स्निपेट में 'UIImageview' के साथ' UIViewview' को बदल दिया है तो यह काम करता है? – freespace

उत्तर

2

.h फ़ाइल में ivar के रूप में अवलोकन के रूप में घोषित करें। सिंथेसाइज़ और फिर इसे इस तरह कहते हैं:

[self.theview setMultipleTouchEnabled:YES]; 

alloc और init कि viewDidLoad विधि में theview के लिए मत भूलना।

यही है।

+1

समस्या का समाधान न करें, क्षमा करें :-( –

19

क्या आपने इसे आजमाया?

[view setUserInteractionEnabled:YES]; 
संबंधित मुद्दे