如何在iOS 7中仅针对一个视图禁用后退手势
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS 7中仅针对一个视图禁用后退手势相关的知识,希望对你有一定的参考价值。
我正在尝试使用以下代码集为视图控制器禁用后退手势。
在FirstViewController.m
,我正在设置interactivePopGestureRecognizer
的代表
- (void) viewWillLoad {
// Other stuff..
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
然后实施<UIGestureRecognizerDelegate>
方法并返回NO
。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return NO;
}
在dealloc中,我将委托设置为nil。 (我已在某处读过,在ios 7中,您必须手动将委托设置为nil)
- (void)dealloc {
self.navigationController.delegate = nil;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
这适用于FirstViewController
。但是当我把SecondViewController
推到这个时,手势也不起作用。如何才能在FirstViewController中禁用手势?
此外,当我弹出FirstViewController
去RootViewController
,然后尝试再次推动FirstViewController
时,我得到对象解除分配错误:
[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280
为什么除了将委托设置为nil之外我还需要做什么?或者我把它放在错误的地方?
在FirstViewController中尝试以下未经测试的代码:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
我最初将这些答案放在接受答案之下的评论中,但我觉得这需要作为获得更多可见性的答案。
通常情况下,您会发现接受的答案不起作用。这是因为在将视图添加到导航控制器的视图层次结构之前可以调用viewWillAppear:
,因此self.navigationController
将成为nil
。因此,在某些情况下可能不会禁用interactivePopGestureRecognizer。你最好在viewDidAppear:
中调用它。
这里的代码将起作用(假设您的视图控制器已正确添加到导航控制器的视图层次结构中):
Objective-C的
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[[self navigationController] interactivePopGestureRecognizer] setEnabled:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[[self navigationController] interactivePopGestureRecognizer] setEnabled:YES];
}
迅速
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
我尝试了以上所有但是他们没有为我工作。所以我试过这个,它适用于IOS7和IOS8。 只需确保您的视图控制器实现此协议,即UIGestureRecognizerDelegate并编写下面给出的代码。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear : animated]; if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled =
没有; self.navigationController.interactivePopGestureRecognizer.delegate = self; }
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) { return NO; } else { return YES; }
}
我发现将手势设置为禁用只是并不总是有效。它确实有效,但对我而言,它只是在我曾经使用过背景后才做到的。第二次它不会触发反馈。此外,正如约翰罗杰斯所说,使用viewDidAppear和viewWillAppear是重要的,因为navigationController否则将是nil。
修复我的是委托手势并实现shouldbegin方法返回NO:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Disable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Enable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
这在xCode 7中对我有用:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.navigationController!.interactivePopGestureRecognizer!.enabled = false
}
override func viewWillDisappear(animated: Bool) {
super.viewDidDisappear(animated)
self.navigationController!.interactivePopGestureRecognizer!.enabled = true
}
对于只有一个视图,我不知道方式......但是我使用下一个代码来完全禁用滑动手势:
在你的AppDelegate.m中
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
以上是关于如何在iOS 7中仅针对一个视图禁用后退手势的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ios 11 中使用主详细信息视图禁用向后滑动手势?