亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS左滑手勢(shì)失效的解決方法

 更新時(shí)間:2017年09月30日 10:52:28   作者:洛洛愛(ài)吃肉  
這篇文章主要為大家詳細(xì)介紹了iOS左滑手勢(shì)失效的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

iOS7之后,蘋果優(yōu)化了一個(gè)小功能,就是對(duì)于UINavagationController堆棧里的UIViewController,只要輕輕在視圖控制器的左邊緣右滑一下,該視圖控制器就會(huì)pop出棧(前提當(dāng)然是對(duì)于非根視圖控制器而言)。實(shí)現(xiàn)方法很簡(jiǎn)單,一句話搞定:

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

事實(shí)上對(duì)于一個(gè)視圖控制器而言,該屬性的默認(rèn)值即為YES,因此不設(shè)置也能實(shí)現(xiàn)右滑pop的功能。

然而這個(gè)功能很有局限性,因?yàn)樗辉试S當(dāng)前視圖控制器自定義了leftBarButtonItem,一旦自定義,右滑功能就會(huì)失效。這里有一個(gè)方法:

self.navigationController.interactivePopGestureRecognizer.delegate = nil;

設(shè)置代理為nil之后即便自定義了leftBarButtonItem也可以右滑pop。

或者,把手勢(shì)的許可打開(kāi) 也可:

self.navigationController.interactivePopGestureRecognizer.enabled = YES ;

事實(shí)上如果自定義了leftBarButtonItem,常用的做法是重新設(shè)置代理:

- (void)viewDidAppear:(BOOL)animated{
   self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

然后實(shí)現(xiàn)手勢(shì)協(xié)議即可:

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer{
  //判斷是否為rootViewController
  if (self.navigationController && self.navigationController.viewControllers.count == 1) {
    return NO;
  }
  return YES;
}

不過(guò)呢,如果我們自定義的返回button只是文字或圖片的話,這樣設(shè)置就可以,不會(huì)失效

  UIBarButtonItem *item = [[UIBarButtonItem alloc]init];
  item.title = @"";
  self.navigationItem.backBarButtonItem = item;

如果是要自定義view的當(dāng)作button的話,就要用leftBarButtonItem設(shè)置,并用上述講的防止手勢(shì)失效的方案.

有朋友提出以上方式在多次滑動(dòng)之后會(huì)導(dǎo)致界面假死,這里再給出一種解決方案:

在所有除一級(jí)頁(yè)面之外的頁(yè)面的viewDidAppear和viewWillDisappear中加入以下代碼:

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  //代理置空,否則會(huì)閃退
  if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
  }
}
- (void)viewDidAppear:(BOOL)animated { 
  [super viewDidAppear:animated]; 
  //開(kāi)啟iOS7的滑動(dòng)返回效果 
  if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { 
    //只有在二級(jí)頁(yè)面生效 
    if ([self.navigationController.viewControllers count] == 2) { 
      self.navigationController.interactivePopGestureRecognizer.delegate = self; 
    } 
  } 
} 

在UINavigationController的delegate中實(shí)現(xiàn)以下方法:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
//開(kāi)啟滑動(dòng)手勢(shì)
  if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    navigationController.interactivePopGestureRecognizer.enabled = YES;
  }
} 

在pushviewcontroller之前加入以下代碼:

//在切換界面的過(guò)程中禁止滑動(dòng)手勢(shì),避免界面卡死
if ([_currentNav respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  _currentNav.interactivePopGestureRecognizer.enabled = NO;
}
[_currentNav pushViewController:viewController animated:YES];

即可在實(shí)現(xiàn)滑動(dòng)返回的同時(shí),避免界面卡死的問(wèn)題。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論