代码改变世界

iOS UI进阶-6.0 手势

2015-12-20 10:20  jiangys  阅读(385)  评论(0编辑  收藏  举报

给每个页面添加手势,只需要统一设置不是根控制器的页面,都增加手势。需要自定义导航控制器

1.继承代理

@interface BSNavigationController ()<UIGestureRecognizerDelegate>

 2.设置代理

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置pop手势代理
    self.interactivePopGestureRecognizer.delegate = self;
    
}

 3.实现代理

#pragma mark - <UIGestureRecognizerDelegate>

/**
 *  push进来的控制器大于1个,手势有效
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    return self.viewControllers.count > 1;
}

 

在开发过程中,经常会用到,需要关闭某个页面的手势返回功能。

// 禁用返回手势
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

// 开启手势
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // 开启手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
// 导航栏返回
- (void)blackBtnClick
{
    // 开启手势,有时只在(void)viewWillDisappear:(BOOL)animated开启,该根栏目下所有的手势都会失效
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

参考网站:http://blog.csdn.net/jasonblog/article/details/28282147