2013-07-18 7 views
9

काम नहीं कर रहा है मुझे एक समस्या है जहां मेरे UILaprollView में मेरे UILabels पर मेरे UILabels पर मेरा UITapGestureRecognizer इसकी विधियों को कॉल नहीं कर रहा है।UIScrollView के सबव्यू में UILabels पर UITapGestureRecognizer

दृश्य पदानुक्रम इस प्रकार है:

  • scrollview (UIScrollView)
    • contentView (UIView)
      • testLabel (UILabel) - यहाँ है जहां UITapGestureRecognizer जुड़ा हुआ है

मैं इस समस्या को उजागर करने के

// Set scrollview size - Added in Storyboad 
[scrollView setContentSize:CGSizeMake([arrayOfVerbs count]*self.view.frame.size.width, scrollView.contentSize.height)]; 
[scrollView setCanCancelContentTouches:YES]; // Tried both yes and no 
[scrollView setPagingEnabled:YES]; 

// Add content view 
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)]; 
[scrollView addSubview:contentView]; 

// Add test UILabel 
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)]; 
[testLabel setBackgroundColor:[UIColor redColor]]; 
[testLabel setText:@"Test touch"]; 
[testLabel setUserInteractionEnabled:YES]; 
[contentView addSubview:testLabel]; 

// Add gesture recogniser 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playSound:)]; 
singleTap.numberOfTapsRequired = 1; 
[testLabel addGestureRecognizer:singleTap]; 

एक उदाहरण के लिए नीचे कोड आसुत है और इस विधि कि टैप जेस्चर recogniser बुलाना चाहिए

- (void)playSound:(UITapGestureRecognizer *)sender { 

    NSLog(@"play sound"); 

    if(sender.state == UIGestureRecognizerStateEnded) 
    { 
     int pronounNumber = [sender.view tag]; 
     int exampleNumber = (int)sender.view.frame.origin.x%(int)self.view.frame.size.width; 

     NSLog(@"Pronoun is %i and example is %i", pronounNumber, exampleNumber); 
    } 
} 

इस विधि कभी नहीं कहा जाता है जब मैं UILabel पर छूने की कोशिश की।

मैंने इस thread द्वारा सुझाए गए स्क्रॉल व्यू पर YES और NO दोनों को संपत्ति कैनकेलकंटेंट टच को सेट करने का प्रयास किया है, लेकिन यह अभी भी काम नहीं कर रहा है।

अजीब बात यह है कि, यदि मैं scrollView के बाहर UILabel जोड़ता हूं, तो इशारा पहचानकर्ता काम करता है! तो समस्या केवल मेरी सामग्री में होती है देखें जो मेरे scrollView का सबव्यूव है।

मैं ऑटो लेआउट का उपयोग कर रहा हूं, अगर यह कोई अंतर हो सकता है?

धन्यवाद!

+2

सेटिंग [सामग्री दृश्य सेटक्लिपबाउंड: हाँ] सेट करने का प्रयास करें; और देखें कि 'ContentView' के अंदर आपका' लेबल' दृश्यमान है, यदि नहीं, तो आपका 'लेबल' स्पर्श क्षेत्र में नहीं है, अपने आकार बदलने वाले गुणों को समायोजित करें .. – iphonic

उत्तर

10

स्क्रॉल व्यू में एक इशारा पहचानकर्ता भी है। डिफ़ॉल्ट रूप से, केवल 1 इशारा पहचानकर्ता किसी भी समय स्पर्श को संभाला जा सकता है। आपको अपने इशारे का प्रतिनिधि बनने की आवश्यकता है और फिर को YES वापस करने के लिए लागू करें। यह स्क्रॉल व्यू के साथ ही एक ही समय में काम करने की अनुमति देगा।

+0

ठीक है, इसलिए मैंने 'singleTap.delegate = self;' सेट किया है और मेरी .h फ़ाइल में 'UIGestureRecognizerDelegate' लागू किया है, और 'इशारा रिकॉग्नाइज़र: चाहिए पहचानना चाहिए साथ-साथ WithGestureRecognizer:', लेकिन यह अभी भी काम नहीं करता है। –

+2

क्या प्रतिनिधि विधि को बुलाया जा रहा है? सामग्री दृश्य का आकार क्या है (इसे लॉग करें)? – Wain

+1

ओएमजी! आपको यह मिला! यह 'सामग्री दृश्य आकार

2

tagGestures के लिए प्रतिनिधि जोड़ें,

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playSound:)]; 
singleTap.numberOfTapsRequired = 1; 
singleTap.delegate = self; 
[testLabel addGestureRecognizer:singleTap]; 

संपादित करें: -

contentView.userInteractionEnabled = YES; 

अपने कोड it'l काम इस लाइन डाल दिया।

+0

@ जोसेफ विलियमसन: एक बार मेरा संपादित हिस्सा। – Balu

+0

हाय, मैंने अभी प्रतिनिधि को स्वयं को सेट करने का प्रयास किया है, और '' प्रोटोकॉल और - '(BOOL) इशारा किया गया है। पहचानकर्ता: (UIGestureRecognizer *) इशारा रिकॉग्नाइज़र चाहिएReceiveTouch: (UITouch *) स्पर्श' विधि।अभी भी काम नहीं करता है, मुझे इसके लिए एक प्रतिनिधि को लागू करने की आवश्यकता क्यों होगी जब मुझे स्क्रॉलव्यू के बाहर UILabels के लिए इसकी आवश्यकता नहीं है? –

+0

@ जोसेफ विलियमसन: क्या आपने मेरे उत्तर में कोड की आखिरी पंक्ति जोड़ दी है? एक बार इसे जोड़ने और जांचें। – Balu

-3
[yourlabel.addGestureRecognizer:tapGestureDeFromage]; 

को आपके लेबल में स्पष्ट रूप से इशारा जोड़ना चाहिए।

संबंधित मुद्दे