13

मैं अपने यूआईवीविगेशन नियंत्रक-आधारित ऐप के माध्यम से अपने बैक बटन पर कार्यक्षमता जोड़ना चाहता हूं जहां बैक बटन को लंबे समय तक दबाकर रूट पर पॉप-अप किया जाएगा। हालांकि, मैं यह नहीं समझ सकता कि इशारा पहचानकर्ता को कहां संलग्न करना है। क्या मैं UINavigationBar को उपclass करता हूं और बाएं बटन क्षेत्र में लंबी प्रेस है या नहीं, कोशिश करें और पता लगाएं?UINavigationItem के बैक बटन पर लंबे समय तक प्रेस का पता लगाएं

मैंने लोगों के बारे में पहले ही समान कार्यक्षमता जोड़ने के बारे में सुना है। क्या किसी के भी पास कोई सुझाव है?

उत्तर

6

मेरा मानना ​​है कि UIGestureRecognizers को केवल UIViews और UIViews के उप-वर्गों में जोड़ा जा सकता है।

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

वापस बटन एक UIBarButtonItem कि NSObject से उतरता है। इसलिए, आप एक मानक वापसी बटन का प्रयोग

UILongPressGestureRecognizer *longPressGesture = 
      [[[UILongPressGestureRecognizer alloc] 
       initWithTarget:self action:@selector(longPress:)] autorelease]; 

[self.navigationItem.backBarButtonItem addGestureRecognizer:longPressGesture]; 

आप फिर भी एक UIBarButtonItem के लिए एक कस्टम दृश्य जोड़ सकते हैं करने के लिए एक इशारा पहचानकर्ता संलग्न करने के लिए सक्षम नहीं होगा। एक कस्टम दृश्य बस के रूप में आसानी से एक UIView, UIButton, UILabel, आदि हो सकता है

उदाहरण:

UIView *myTransparentGestureView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,30)]; 
[myTransparentGestureView addGestureRecognizer:longPressGesture]; 
[self.navigationItem.backBarButtonItem setCustomView:myTransparentGestureView]; 
// Or you could set it like this 
// self.navigationItem.backBarButtonItem.customView = myTransparentGestureView; 
[myTransparentGestureView release]; 

आप फिर भी सावधान रहने की, backBarButtonItem पर गुण स्थापित करने के बाद से है अगले दृश्य है कि आप पुश करने के लिए लागू होता है । इसलिए यदि आपके पास ए देखने के लिए धक्का है जो बी को देखने के लिए धक्का देता है और जब आप बैक को वापस टैप करते हैं तो आपको इशारा होना चाहिए। आपको इसे ए

+0

मेरे लिए काम नहीं करता ... पहचानकर्ता आग से इंकार कर देता है। क्या आप इसे ऊपर दिए गए कोड के साथ काम करने में सक्षम थे? – kevboh

+0

यह संभवतः काम नहीं कर रहा है क्योंकि बैकबर्टबटन इटिम केवल पढ़ा जाता है, इसलिए यह कस्टम दृश्य स्वीकार नहीं कर रहा है।आपको इस उत्तर की तरह अपने स्वयं के बाएंबारबटनटन बनाने की आवश्यकता होगी। http://stackoverflow.com/questions/526520/how-to-create-backbarbuttomitem-with-custom-view-for-a-uinavigationcontroller – Andrew

+0

आह, लेकिन तब तक मैं अपना पिछला तीर खो देता हूं जब तक कि मुझे कोई छवि न मिल जाए ... शायद इसके लायक नहीं। वैसे भी, धन्यवाद! – kevboh

15

पता है कि यह प्रश्न पुराना है, लेकिन मैं आया एक समाधान के साथ। बटन पर इशारा पहचानकर्ता जोड़ने की कोशिश करने के बजाय (जो आदर्श होगा), मैंने इसे self.navigationController.navigationBar में जोड़ा और फिर कार्रवाई विधि में, locationInView का उपयोग यह देखने के लिए करें कि मैं बैक बटन पर हूं या नहीं। मुझे पूरी तरह से बैक बटन की पहचान करने के तरीके के बारे में पूरी तरह से यकीन नहीं था, इसलिए मैं कुछ मनमानी मूल्य से कम एक्स समन्वय के साथ पहले सबव्यू को पकड़ रहा हूं, लेकिन ऐसा लगता है कि यह वादा करता है। अगर किसी के पास बैक बटन के फ्रेम की पहचान करने का बेहतर तरीका है, तो मुझे बताएं।

- (void)longPress:(UILongPressGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateEnded) 
    { 
     // set a default rectangle in case we don't find the back button for some reason 

     CGRect rect = CGRectMake(0, 0, 100, 40); 

     // iterate through the subviews looking for something that looks like it might be the right location to be the back button 

     for (UIView *subview in self.navigationController.navigationBar.subviews) 
     { 
      if (subview.frame.origin.x < 30) 
      { 
       rect = subview.frame; 
       break; 
      } 
     } 

     // ok, let's get the point of the long press 

     CGPoint longPressPoint = [sender locationInView:self.navigationController.navigationBar]; 

     // if the long press point in the rectangle then do whatever 

     if (CGRectContainsPoint(rect, longPressPoint)) 
      [self doWhatever]; 
    } 
} 

- (void)addLongPressGesture 
{ 
    if (NSClassFromString(@"UILongPressGestureRecognizer")) 
    { 
     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 
     [self.navigationController.navigationBar addGestureRecognizer:longPress]; 
     [longPress release]; 
    } 
} 
+0

कूल विचार। अगली बार मुझे ऐसा करने की ज़रूरत है, मैं आपके दृष्टिकोण को आजमाउंगा। पालन ​​करने के लिए धन्यवाद! – kevboh

+0

मैं पॉप पर बस किसी भी लंबे प्रेस के साथ ठीक हूं - मुझे उचित लगता है। –

+0

मुझे वही आवश्यकता थी जैसे बैक बटन पर एक लंबी प्रेस जेस्चर सेट करना। इस दृष्टिकोण के साथ समस्या यह है कि बैक बटन हाइलाइट किए गए राज्य में नहीं रहता है। इसलिए मैंने cancelsTouchesInView प्रोपरी को NO.BUt पर सेट किया है, फिर मेरे longPress हैंडलर और बैक बटन हैंडलर बुलाया जाता है। इसके लिए कोई समाधान ?? – user1010819

3

मैंने थोड़ा अलग रास्ता पीछा किया, मुझे लगा कि मैं इसे साझा करूंगा। इसके बाद के संस्करण जवाब ठीक हैं, लेकिन वास्तव में, अगर देर तक दबाए रखने नेविगेशन बार के अग्रणी 1/3 में है, कि काफी अच्छा मेरे लिए है:

AppDelegate में (:

- (void)longPress:(UILongPressGestureRecognizer *)gr 
{ 
    NSLog(@"longPress:"); 
    UINavigationBar *navBar = [self navigationBar]; 
    CGFloat height = navBar.bounds.size.height; 
    CGPoint pt = [gr locationOfTouch:0 inView:navBar]; 
    //NSLog(@"PT=%@ height=%f", NSStringFromCGPoint(pt), height); 
    if(CGRectContainsPoint(CGRectMake(0,0,100,height), pt)) { 
     [self popToViewController:self.viewControllers[0] animated:YES]; 
    } 
} 
0

यहाँ मेरी समाधान है मेरे एप्लिकेशन में नेविगेशन बार की "मालिक"), applicationDidFinishLaunchingWithOptions में:

नेविगेशन बार दृश्य प्राप्त करें और पूरे दृश्य को इशारा पहचानकर्ता जोड़ें: appDel में फिर

// Get the nav bar view 
UINavigationBar *myNavBar = nil; 
for (UIView *view in [self.window.rootViewController.view subviews]) { 
    if ([view isKindOfClass:[UINavigationBar class]]) { 
     NSLog(@"Found Nav Bar!!!"); 
     myNavBar = (UINavigationBar *)view; 
    } 
} 

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                         action:@selector(backButtonLongPress:)]; 
[myNavBar addGestureRecognizer:longPress]; 
NSLog(@"Gesture Recognizer Added."); 

eGate, में - (शून्य) backButtonLongPress: (आईडी) इस

अगर इशारा वापस बटन की सीमा के भीतर होता है चेक देखने के लिए: पर backButtomItem नहीं था एक कस्टम दृश्य करने के लिए एक इशारा पहचानकर्ता जोड़ना

if ([sender state] == UIGestureRecognizerStateBegan) { 

    // Get the nav bar view 
    UINavigationBar *myNavBar = nil; 
    for (UIView *view in [self.window.rootViewController.view subviews]) { 
     if ([view isKindOfClass:[UINavigationBar class]]) { 
      NSLog(@"Found Nav Bar!!!"); 
      myNavBar = (UINavigationBar *)view; 
     } 
    } 

    // Get the back button view 
    UIView *backButtonView = nil; 
    for (UIView *view in [myNavBar subviews]) { 
     if ([[[view class] description] isEqualToString:@"UINavigationItemButtonView"]) { 
      backButtonView = view; 
      NSLog(@"Found It: %@", backButtonView); 
      NSLog(@"Back Button View Frame: %f, %f; %f, %f", backButtonView.frame.origin.x, backButtonView.frame.origin.y, backButtonView.frame.size.width, backButtonView.frame.size.height); 
     } 
    } 

    CGPoint longPressPoint = [sender locationInView:myNavBar]; 
    NSLog(@"Touch is in back button: %@", CGRectContainsPoint(backButtonView.frame, longPressPoint) ? @"YES" : @"NO"); 
    if (CGRectContainsPoint(backButtonView.frame, longPressPoint)) { 
     // Place your action here 
    } 

    // Do nothing if outside the back button frame 

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