方法一 OC:
- (void)viewDidLoad {
[super viewDidLoad];
id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
[self.view addGestureRecognizer:pan];
}
方法二 Swift:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.isIdleTimerDisabled = true
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false;
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
UIApplication.shared.isIdleTimerDisabled = false
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true;
}
方法三:
- 先寫一個UIViewController分類
+ (void)popGestureClose:(UIViewController *)VC
{
// 禁用側(cè)滑返回手勢
if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//這里對添加到右滑視圖上的所有手勢禁用
for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
popGesture.enabled = NO;
}
}
}
+ (void)popGestureOpen:(UIViewController *)VC
{
// 啟用側(cè)滑返回手勢
if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//這里對添加到右滑視圖上的所有手勢啟用
for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
popGesture.enabled = YES;
}
}
}
- 再在控制器里使用
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIViewController popGestureClose:self];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[UIViewController popGestureOpen:self];
}
參考
iOS 禁止某個頁面?zhèn)然祷?/a>
iOS 禁用右滑返回手勢
iOS右滑返回手勢深度全解和最佳實施方案
希望后人珍惜時間,少走彎路,享受生活肖粮。
VKOOY