13

Geri düğmelerim, UINavigationController tabanlı uygulama aracılığıyla arka tuşlara işlev eklemek istiyorum. Ancak, jest tanıyıcıyı nereye ekleyeceğimi anlayamıyorum. UINavigationBar'ın alt sınıfını mı seçiyorum ve uzun basının sol düğme bölgesinde olup olmadığını tespit edip deneyeyim mi?UINavigationItem'in geri düğmesine uzun süreli basın algılaması

Daha önce benzer işlevleri ekleyen kişilerin işittiğini duydum. Herhangi bir fikri olan var mı?

cevap

6

UIGestureRecognizers'in yalnızca UIViews ve UIViews alt sınıflarına eklenebileceğine inanıyorum.

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

geri düğmesi NSObject iner bir UIBarButtonItem olduğunu. Bu nedenle,

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

[self.navigationItem.backBarButtonItem addGestureRecognizer:longPressGesture]; 

Bununla birlikte bir UIBarButtonItem için özel bir görünüm ekleyebilirsiniz kullanarak standart bir geri düğmesi bir jest tanıyıcı eklemek mümkün olmayacaktır. Özel bir görünüm aynı kolaylıkla vb bir UIView, UIButton, UILabel, olabilir

Örnek:

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]; 
Sen backBarButtonItem özellikleri ayarlayarak beri Ancak dikkatli olmak zorunda

itmeye sonraki görünümüne geçerlidir . Bu yüzden, B'yi görüntülemek için iter ve A'yı görünümde B'ye dokunduğunuzda tanının tanınmasını istersiniz. Görüntünün A'ya göre ayarlanması gerekir.

+0

Benim için çalışıyorum ... tanıyıcı ateş etmeyi reddediyor. Yukarıdaki kodla çalışmayı başarabildiniz mi? – kevboh

+0

Muhtemelen backBarButtonItem salt okunur olduğundan çalışmaz, bu yüzden özel bir görünüm kabul etmiyor.Muhtemelen bu cevap gibi kendi sol çubuğunuzda oluşturmalısınız. http://stackoverflow.com/questions/526520/how-to-create-backbarbuttomitem-with-custom-view-for-a-uinavigationcontroller – Andrew

+0

Ah, ama sonra bir imge bulmazsam arka okumu kaybediyorum. Değmez. Yine de teşekkürler! – kevboh

15

Bu sorunun eski olduğunu biliyorum, ama geldim bir çözüm ile. Hareket algılayıcısını düğmenin kendisine eklemeye çalışmak yerine (hangisi ideal olur), onu self.navigationController.navigationBar'a ekledim ve daha sonra eylem yönteminde geri düğmesinin üstündeyim mi diye görmek için locationInView'u kullanın. Geri butonunu tam olarak nasıl belirleyeceğime dair tam olarak emin değildim, bu yüzden ilk alt görünümü sadece bazı rasgele değerlerden daha az bir x koordinatıyla kapıyorum, ama umut verici görünüyor. Birisinin geri düğmesinin çerçevesini tanımlamak için daha iyi bir yolu varsa, bana bildirin.

- (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

Harika bir fikir. Bir dahaki sefere bunu yapmalıyım yaklaşımınızı deneyeceğim. Takip için teşekkürler! – kevboh

+0

Ben sadece pop için herhangi bir uzun basın ile iyiyim - bana makul görünüyor. –

+0

Geri düğmesi üzerinde bir longPress Jest ayarlama gibi aynı gereksinimi vardı.Bu yaklaşım, arka düğme vurgulanmış durumda kalmamasıdır.Yani ben de benim longPressHandler ve backButton işleyici benim NO.BUt iptalleriTouchesInView propery ayarlayın buna denir. Bunun için herhangi bir çözüm var mı? – user1010819

3

Biraz farklı bir yol izledim, paylaşacağımı düşündüm. Yukarıdaki cevaplar iyidir, ancak uzun basın gezinme çubuğunda lider 1/3 ise gerçekten, bu benim için yeterince iyi: appDelegate yılında

(:

İşte
- (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

benim çözüm applicationDidFinishLaunchingWithOptions ise uygulamamda gezinme çubuğunda "sahibi"),:

nav çubuğu görünüm elde edin ve tüm görünümüne jest tanıyıcı ekleyin: appDel içinde Sonra

// 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, içinde - (void) backButtonLongPress: (id) gönderen

jest geri düğmesi çerçevesinde oluşursa kontrol edin: backButtomItem değil mi üzerine özel bir görünüm için jest tanıyıcı ekleme

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 

} 
İlgili konular