2011-09-09 7 views
8

मेरे पास कई UIButtons के साथ एक दृश्य है। मैंने चयनकर्ता के रूप में निम्नलिखित के साथ UILongPressGestureRecognizer का उपयोग करके सफलतापूर्वक कार्यान्वित किया है;UILongPressGestureRecognizer पर मैं कैसे पता लगा सकता हूं कि कौन सा ऑब्जेक्ट ईवेंट उत्पन्न करता है?

- (void)longPress:(UILongPressGestureRecognizer*)gesture { 
    if (gesture.state == UIGestureRecognizerStateEnded) { 
     NSLog(@"Long Press"); 
    } 
} 

मुझे पता है के भीतर इस विधि के बाद से मैं कुछ अलग, पर निर्भर करते हुए बटन पाठ और ईमेल प्राप्त करने की जरूरत है जो UIButton पाठ और ईमेल प्राप्त है की जरूरत है।

उम्मीद है कि उत्तर बटनों की सीमाओं पर लम्बे समय से होने वाले निर्देशांक मैपिंग का कुछ मुद्दा नहीं है - बल्कि वहां नहीं जाना होगा।

कोई सुझाव?

धन्यवाद!

उत्तर

10

यह gesture.view में उपलब्ध है।

// Get the position of the point tapped in the window co-ordinate system 
CGPoint tapPoint = [gesture locationInView:nil]; 

UIView *viewAtBottomOfHeirachy = [self.window hitTest:tapPoint withEvent:nil]; 

if ([viewAtBottomOfHeirachy isKindOfClass:[UIButton class]]) 
1

आपके विचार (बटन के बहुत सारे की तरह) कई subviews आप निर्धारित कर सकते क्या उपयोग किया गया था मौजूद होता है तो? यदि ऐसा है, तो @ मैजिक बुलेट डेव के दृष्टिकोण के साथ कुछ संभवतः जाने का तरीका है।

एक विकल्प UIButton को उपclass करना है और प्रत्येक UIButton को एक लंबा टैपगेशर रिकॉग्निज़र में जोड़ना है। फिर आप अपना बटन जो चाहें उसे करने के लिए प्राप्त कर सकते हैं। उदाहरण के लिए, यह एक दृश्य नियंत्रक को स्वयं को पहचानने वाला एक संदेश भेज सकता है। निम्नलिखित स्निपेट उप-वर्ग के लिए विधियों को दिखाता है।

- (void) setupLongPressForTarget: (id) target; 
{ 
    [self setTarget: target]; // property used to hold target (add @property and @synthesise as appropriate) 

    UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:button action:@selector(longPress:)]; 
    [self addGestureRecognizer:longPress]; 
    [longPress release]; 
} 

- (void) longPress: (UIGestureRecognizer*) recogniser; 
{ 
    if (![recogniser isEnabled]) return; // code to prevent multiple long press messages 
    [recogniser setEnabled:NO]; 
    [recogniser performSelector:@selector(setEnabled:) withObject: [NSNumber numberWithBool:YES] afterDelay:0.2]; 

    NSLog(@"long press detected on button"); 

    if ([[self target] respondsToSelector:@selector(longPressOnButton:)]) 
    { 
     [[self target] longPressOnButton: self]; 
    } 
} 

आप इस तरह कोड कुछ हो सकता है आपके विचार नियंत्रक में:

- (void) viewDidLoad; 
{ 
    // set up buttons (if not already done in Interface Builder) 

    [buttonA setupLongPressForTarget: self]; 
    [buttonB setupLongPressForTarget: self]; 

    // finish any other set up 
} 

- (void) longPressOnButton: (id) sender; 
{ 
    if (sender = [self buttonA]) 
    { 
     // handle button A long press 
    } 

    if (sender = [self buttonB]) 
    { 
     // handle button B long press 
    } 

    // etc. 

} 
3

आप UIView subviews के रूप में UIButtons है कि करने के लिए लंबे समय से टैप जेस्चर नियंत्रक जोड़ रहे हैं:

+0

अगर ([[आत्म लक्ष्य] respondsToSelector: @selector (longPressOnButton :)]) { [[आत्म लक्ष्य] longPressOn बटन: स्वयं]; } यह if-block संकलित नहीं करता है ... त्रुटि के रूप में: कोई ज्ञात उदाहरण विधि ... – trillions

+0

मैंने इसे समझ लिया ... एक प्रोटोकॉल जोड़ा और यह तय किया गया। धन्यवाद.. – trillions