iOS开发中如何拦截侧滑返回手势
时间: 2023-01-28 08:25:59 浏览: 62
在 iOS 开发中,可以通过实现 UINavigationControllerDelegate 协议中的 navigationController:didShowViewController:animated: 方法来拦截侧滑返回手势。
例如,你可以在这个方法中判断当前展示的视图控制器是否需要禁用侧滑返回手势,如果需要,则设置导航控制器的 interactivePopGestureRecognizer.enabled 属性为 NO,否则设置为 YES。
示例代码如下:
```
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
BOOL shouldEnableGesture = YES;
if ([viewController conformsToProtocol:@protocol(DisablePopGestureProtocol)]) {
shouldEnableGesture = [viewController disablePopGesture];
}
navigationController.interactivePopGestureRecognizer.enabled = shouldEnableGesture;
}
```
在这段代码中,我们新增了一个协议 DisablePopGestureProtocol,用于标记那些视图控制器需要禁用侧滑返回手势。在每个视图控制器中,如果希望禁用侧滑返回手势,则只需要实现这个协议中的 disablePopGesture 方法并返回 NO 即可。
希望这些信息对你有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)