iOS Swift App用WKWebView加载h5, h5多

2022-07-13  本文已影响0人  Lee坚武
本人亲测有效!更多交流可以家魏鑫:lixiaowu1129,公重好:iOS过审汇总,一起探讨iOS相关技术!
1. 创建webView时使用:作用是允许h5多页面,手势滑动
wkWebView.allowsBackForwardNavigationGestures = true
2. 因为App系统自带侧滑手势返回,所以要这样做:
//监听侧滑手势
let swiperGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleNavigationTransition(_:)))
swiperGesture.delegate = self // 设置手势代理,拦截手势触发
view.addGestureRecognizer(swiperGesture)
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
navigationController?.interactivePopGestureRecognizer?.delegate = self

第二种方法:

// handleNavigationTransition:为系统私有API,即系统自带侧滑手势的回调方法,我们在自己的手势上直接用它的回调方法
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleNavigationTransition:)];
    panGesture.delegate = self; // 设置手势代理,拦截手势触发
    [self.view addGestureRecognizer:panGesture];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
需求是左滑手势调用h5返回上一级,逐级返回。利用wkWebView.canGoBack属性实现
- (void)handleNavigationTransition:(UIGestureRecognizer *)gap{
    //    NSLog(@"用户左滑了手势啊");
    if (_wkWebView.canGoBack==YES) {
        [_wkWebView goBack];
    }else{
        NSLog(@"此时不能左滑");
    }
}

本文由mdnice多平台发布

上一篇下一篇

猜你喜欢

热点阅读