一般百度搜索都是这种方法,
- (void)setGloblePopGes
{
// 获取系统自带滑动手势的target对象
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
// 创建全屏滑动手势,调用系统自带滑动手势的target的action方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// 设置手势代理,拦截手势触发
pan.delegate = self;
// 给导航控制器的view添加全屏滑动手势
[self.view addGestureRecognizer:pan];
// 禁止使用系统自带的滑动手势
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 注意:只有非根控制器才有滑动返回功能,根控制器没有。
// 判断导航控制器是否只有一个子控制器,如果只有一个子控制器,肯定是根控制器
if (self.childViewControllers.count == 1) {
// 表示用户在根控制器界面,就不需要触发滑动手势,
return NO;
}
return YES;
}
- (void)handleNavigationTransition:(UIPanGestureRecognizer *)sender {
//此处写一个空的方法 不做任何操作
}
然而我在我项目中写这种方法并没有用,可能是同事改了里面的基类还是怎么,后来发现了一种便捷方法,iOS中的webView禁止手势返回
#import "FlexibilityViewController.h"
@interface FlexibilityViewController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>
{
UIWebView *webView;
UIGestureRecognizer *_pan;
}
@end
@implementation FlexibilityViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
view.userInteractionEnabled = YES;
[self.view addSubview:view];
webView = [[UIWebView alloc]initWithFrame:view.frame];
[webView setUserInteractionEnabled:YES];//是否支持交互
webView.delegate = self;
webView.scrollView.bounces = NO; //禁止webView滑动和回弹
[webView setOpaque:NO];//opaque是不透明的意思
[webView setScalesPageToFit:YES];//自动缩放以适应屏幕
[view addSubview:webView];
NSURL *url = [NSURL URLWithString:@"http://120.24.159.3/bb.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:_pan];
}
- (BOOL)panAction:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
@end