一般百度搜索都是這種方法权悟,
- (void)setGloblePopGes
{
// 獲取系統(tǒng)自帶滑動(dòng)手勢(shì)的target對(duì)象
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
// 創(chuàng)建全屏滑動(dòng)手勢(shì)锌订,調(diào)用系統(tǒng)自帶滑動(dòng)手勢(shì)的target的action方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// 設(shè)置手勢(shì)代理,攔截手勢(shì)觸發(fā)
pan.delegate = self;
// 給導(dǎo)航控制器的view添加全屏滑動(dòng)手勢(shì)
[self.view addGestureRecognizer:pan];
// 禁止使用系統(tǒng)自帶的滑動(dòng)手勢(shì)
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// 注意:只有非根控制器才有滑動(dòng)返回功能急膀,根控制器沒有沮协。
// 判斷導(dǎo)航控制器是否只有一個(gè)子控制器,如果只有一個(gè)子控制器卓嫂,肯定是根控制器
if (self.childViewControllers.count == 1) {
// 表示用戶在根控制器界面皂股,就不需要觸發(fā)滑動(dòng)手勢(shì),
return NO;
}
return YES;
}
- (void)handleNavigationTransition:(UIPanGestureRecognizer *)sender {
//此處寫一個(gè)空的方法 不做任何操作
}
然而我在我項(xiàng)目中寫這種方法并沒有用命黔,可能是同事改了里面的基類還是怎么呜呐,后來(lái)發(fā)現(xiàn)了一種便捷方法,iOS中的webView禁止手勢(shì)返回
#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滑動(dòng)和回彈
[webView setOpaque:NO];//opaque是不透明的意思
[webView setScalesPageToFit:YES];//自動(dòng)縮放以適應(yīng)屏幕
[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