ios 導航問題

來源:互聯網
上載者:User

標籤:should   習慣   failure   return   png   receive   base   att   vba   

  8   9 一、手勢右滑返回效果失效 10  11 在我們經常使用的APP中,已經習慣右滑返回這個效果,可是我發現我們的APP中這個功能失效了,只能點擊左上方的返回按鈕才能執行返回這個效果。後來查了一下發現,導致這個問題的原因是因為我們自己自訂了左上方的leftBarButtonItem,我們自訂了這個BarButtonItem使得系統不能捕獲pop手勢了。 12  13 解決方案: 14  15 建立一個UINavigationController的子類LGJBaseNavController,該類@interface LGJBaseNavController (),所有的關於導航控制器的操作都在這個類裡面操作。 16  17 1 設定手勢的delegate為self,導航控制器的delegate也為self。 18  19 - (void)viewDidLoad { 20     [super viewDidLoad]; 21   22     __weak LGJBaseNavController *weakSelf = self; 23     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { 24         self.interactivePopGestureRecognizer.delegate = weakSelf; 25         self.delegate = weakSelf; 26     } 27 } 28   29 - (UIViewController *)popViewControllerAnimated:(BOOL)animated { 30     return [super popViewControllerAnimated:animated]; 31 } 32 2 在轉場/過渡的時候禁用 interactivePopGestureRecognizer當使用者在轉場的時候觸發一個後退手勢,這時候容易各種其他事件也會被喚醒,導航棧或邊混亂。那麼在轉場效果的過程中禁用手勢識別,當新的視圖控制器載入完成後再啟用。 33  34 #pragma mark - UINavigationControllerDelegate 35 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 36     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { 37         self.interactivePopGestureRecognizer.enabled = NO; 38     } 39     //設定返回按鈕 40     if (self.viewControllers.count > 0) { 41         viewController.navigationItem.leftBarButtonItem = [self backButtonItem]; 42     } 43     [super pushViewController:viewController animated:animated]; 44 } 45   46 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 47     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { 48         self.interactivePopGestureRecognizer.enabled = YES; 49     } 50 } 51 3 使navigationcontroller中第一個控制器不響應右滑pop手勢 52  53 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 54     if ([self.childViewControllers count] == 1) { 55         return NO; 56     } 57     return YES; 58 } 59 4 解決多個手勢衝突 同時接受多個手勢 60  61 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 62     return YES; 63 } 64 5 解決在手指滑動時候,被pop的viewController中的UIscrollView會跟著一起滾動 65  66 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 67     return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class]; 68 } 69 關於這個處理複雜手勢衝突的方法有幾個類似的手勢代理方法: 70  71 //手指觸控螢幕幕後回調的方法,返回NO則不再進行手勢識別,方法觸發等 72 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch; 73 //開始進行手勢識別時調用的方法,返回NO則結束,不再觸發手勢 74 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer; 75 //是否支援多時候觸發,返回YES,則可以多個手勢一起觸發方法,返回NO則為互斥 76 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer; 77 //下面這個兩個方法也是用來控制手勢的互斥執行的 78 //這個方法返回YES,第一個手勢和第二個互斥時,第一個會失效 79 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0); 80 //這個方法返回YES,第一個和第二個互斥時,第二個會失效 81 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0); 82 可以將這幾個方法寫在一個UINavigationController的子類裡面,然後在AppDelegate中設定self.window.rootViewController = [[LGJBaseNavController alloc] initWithRootViewController:vc1];可以處理全域的導航控制器關於這個右滑手勢返回失效的問題,我以上寫的這些方法直接寫在裡面就可以。另外說一句,在這個LGJBaseNavController中可以設定導覽列的一些自訂樣式:比如這種樣式就可以在LGJBaseNavController初始化中設定: 83  84 668737-e1c9f390f405bd70.png 85  86 navBar.png 87  88  89 + (void)initialize { 90   91     //bar樣式 92     UINavigationBar *bar = [UINavigationBar appearance]; 93     [bar setBarStyle:UIBarStyleDefault]; 94     [bar setBarTintColor:[UIColor blackColor]]; 95   96     [bar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]]; 97   98     //barButton樣式 99     UIBarButtonItem *item = [UIBarButtonItem appearance];100  101     //Normal102     NSMutableDictionary *textAtts = [NSMutableDictionary dictionary];103     textAtts[NSForegroundColorAttributeName] = [UIColor orangeColor];104     textAtts[NSFontAttributeName] = [UIFont systemFontOfSize:13];105     [item setTitleTextAttributes:textAtts forState:UIControlStateNormal];106  107     //不可用狀態108     NSMutableDictionary *disableTextAtts = [NSMutableDictionary dictionary];109     disableTextAtts[NSForegroundColorAttributeName] = [UIColor colorWithRed:123/255.0 green:123/255.0 blue:123/255.0 alpha:1];110     disableTextAtts[NSFontAttributeName] = [UIFont systemFontOfSize:13];111     [item setTitleTextAttributes:disableTextAtts forState:UIControlStateDisabled];112 }113 二、隱藏NavigationBar返回時,上面會有空缺114 115 這個應該也是我們經常見的效果,比如vc1跳轉vc2,vc2的導覽列是隱藏的,當從vc2返回vc1時,在這個過程中,放慢看上面會有缺失一塊,放快了看就是閃屏,這個效果對使用者也是不友好的,這個解決方案比較簡單。就是在次級viewControleller中在設定setNavigationBarHidden時這樣設定: <重要的是後面的animated參數>116 117 118 - (void)viewWillAppear:(BOOL)animated {119     [super viewWillAppear:animated];120     [self.navigationController setNavigationBarHidden:YES animated:animated];121 }122  123 - (void)viewWillDisappear:(BOOL)animated {124     [super viewWillDisappear:animated];125     [self.navigationController setNavigationBarHidden:NO animated:animated];126 }

 

ios 導航問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.