源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android NavigationController 右滑手势详解

  • 时间:2020-03-31 21:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android NavigationController 右滑手势详解
苹果一直都在人机交互中尽力做到极致,在iOS7中,新增加了一个小小的功能,也就是这个api:self.navigationController.interactivePopGestureRecognizer.enabled = YES; 这个api功能就是在NavigationController堆栈内的UIViewController可以支持右滑手势,也就是不用点击右上角的返回按钮,轻轻在屏幕左边一滑,屏幕就会返回,随着ios设备屏幕的增大,这个小功能让手指短,拇指大和手残人士看到了福音。 这个功能是好,但是经常我们会有需求定制返回按钮,如果手动定制了返回按钮,这个功能将会失效,也就是自定义了navigationItem的leftBarButtonItem,那么这个手势就会失效。 解决方法找到两种 [b]1.重新设置手势的delegate[/b]
[u]复制代码[/u] 代码如下:
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
[b]2.当然你也可以自己响应这个手势的事件[/b]
[u]复制代码[/u] 代码如下:
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];
有更多方法以后继续补充,这里可以根据自己需要进行选择,如果只是简单定制了返回按钮,第一种最简单,一句代码搞定。 问题二:ios开发 向右滑动手势实现返回.在NavigationController中如何设置 在navigationController中实现向右滑动 返回功能 系统提供的backbarbuttonitem,不用添加任何代码即可实现向右滑动后退功能,但是往往要对按钮修改样式等时,就需要自定义leftbarbuttonitem,此时向右滑动即失效.通过下面方法即可解决该问题.(本人亲自实验过) 主要是通过设置navigationController.interactivePopGestureRecognizer 此手势的一些属性,此手势大家可以通过sdk查看说明,这里不细说 [b]1. self.navigationController.interactivePopGestureRecognizer.enabled = YES | NO;      手势有效与否[/b] [b]2. self.navigationController.interactivePopGestureRecognizer.delegate = self;               手势的代理,一般会设置为self[/b] 1中的属性,再viewcontroller中默认的设置为YES,即手势有效.按照2中的属性设置后,当前的viewcontroller即可以实现该向右滑动后退功能,但是当回到navigationController的rootView中再次做出向右滑动时,程序会有问题(再次push子controller时,程序卡在当前界面无法跳转).有效解决方案如下: 说明:有两个controllerA,B
navigationController的rootview设置为A,在A中点击按钮后push B.在A的 -(void)viewDidAppear:(BOOL)animated方法中加入self.navigationController.interactivePopGestureRecognizer.enabled = NO;代码如下:
- (void)viewDidAppear:(BOOL)animated
{
  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;  //让rootView禁止滑动
  }
}
[b]然后再B中的- (void)viewDidLoad方法中加入[/b]
- (void)viewDidLoad
{
  // 配置返回按钮
  UIBarButtonItem * backItem = [self barButtonForImageNames:@[@"icon-返回", @"", @""] action:@selector(popBack)];
  backItem.title = @"";
  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
  }
  self.navigationItem.leftBarButtonItem = backItem;

  if ([[[UIDevice currentDevice] systemVersion] floatValue]
 >= 7.0) {

    self.navigationController.interactivePopGestureRecognizer.delegate = self; 
  }
}
这样即可以保证再A中向右滑动后再次pushB时不会卡在A界面. 再项目中大家一般会创建风格统一的界面,一般都会创建一个基础viewcontroller,再此viewcontroller扩展一个配置leftbarbutton的方法,在该方法中加入B的viewDidLoad中的代码,这样在创建leftbarbutton的同时,直接加了返回的操作(不用每个viewController中都加入这样一段代码).然后只在navigationController的rootView中加入A的(void)viewDidAppear:(BOOL)animated方法即可. 以上内容是Android NavigationController 右滑手势详解,希望大家喜欢。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部