2013-09-26 29 views
4

我有一個自定義控件,包含一排按鈕,模仿標籤欄。當UINavigationController遠離根視圖控制器時,此控件滑出視圖,並在導航到根目錄時滑回。與interactivePopGestureRecognizer一起調整位置

在iOS 7中,有​​可以提供滑動後退手勢。我正在修改我的自定義控件,以便滑動量對應於​​的翻譯。

問題是,當用戶釋放觸摸時,如何判斷UINavigationController是否將導航回退或反彈回原始視圖?

[self.interactivePopGestureRecognizer addTarget:self action:@selector(panningBack:)]; 


- (void) panningBack:(UIPanGestureRecognizer *)recognizer 
{ 
    // Snipped - Code that reads the recognizer translation and adjust custom control y position 

    if (recognizer.state == UIGestureRecognizerStateEnded) 
    { 
     // Question: Does it go back, or does it not? 

     // If it goes back, slide custom control into view 
     // Else slide custom control out of view 
    } 
} 

回答

1

我會說最簡單的解決方案是使用導航控制器中實現的默認手勢。當視圖出現時顯示欄並在欄消失時隱藏欄。

知道是否應該返回的優雅解決方案是檢測最後一次移動。

含義,如果用戶去一些像素的左側和發佈 - >反彈 如果用戶去一些像素的權利和發佈 - >顯示以前的控制器

這可以,如果你保存的地點進行在狀態改變並且比較它到狀態結束。

獲得這樣的點:

CGPoint點= [識別locationInView:視圖]。

+1

如何知道導航是否會返回?當用戶釋放手指時? 我的想法是用導航動畫動畫我的自定義標籤欄。 –

+1

我最近發現了Pan Gesture識別器的一個更簡單的解決方案:您可以使用velocity屬性:[recognitionizer velocityInView:self.view]。如果x值爲正值=向右移動=返回 – Nilz11

4

我知道這是一個相當古老的問題,所以答案可能不適用於OP,但也許對其他人有用。我昨天遇到了同樣的問題,並且在網絡的其他部分進行了大量搜索,但沒有發現任何內容。 所以這裏是我用於類似問題的解決方案。這是在navigationcontroller委託中實現的,但我想你可以在其他地方做,如果這更適合你的需要。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{ 
    id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator; 
    [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) { 
     NSLog(@"DONE!!!"); 
     NSLog(@"Container View: %@", [context containerView]); 
     NSLog(@"From VC: %@", [context viewControllerForKey:UITransitionContextFromViewControllerKey]); 
     NSLog(@"To VC: %@", [context viewControllerForKey:UITransitionContextToViewControllerKey]); 
     NSLog(@"Initially Interactive: %i", [context initiallyInteractive]); 
     NSLog(@"Completion Curve: %d", [context completionCurve]); 
     NSLog(@"Is Animated: %i", [context isAnimated]); 
     NSLog(@"Is Cancelled: %i", [context isCancelled]); 
     NSLog(@"Is Interactive: %i", [context isInteractive]); 
     NSLog(@"Percent Complete: %f", [context percentComplete]); 
     NSLog(@"Presentation Style: %d", [context presentationStyle]); 
     NSLog(@"Transition Duration: %f", [context transitionDuration]); 
    }]; 
} 

當用戶擡起她的手指並且動畫相反或完成時,這將會觸發。 [context isCancelled];會告訴你它是否顛倒或完成。在上下文對象中還有很多其他很好的信息可以使用。

+0

令人難以置信的幫助。我非常沮喪,Apple沒有提供任何關於如何檢查流行手勢是否被取消的文檔(或者他們只是假設我們會了解過渡協調員)。輝煌。 – ninjaneer