Ios7 如何在iOS 7中仅对一个视图禁用后退手势
我正在尝试使用以下代码集禁用视图控制器的后退手势 在Ios7 如何在iOS 7中仅对一个视图禁用后退手势,ios7,gesture,back,Ios7,Gesture,Back,我正在尝试使用以下代码集禁用视图控制器的后退手势 在FirstViewController.m中,我正在设置interactiveepogesturerecognizer - (void) viewWillLoad { // Other stuff.. self.navigationController.interactivePopGestureRecognizer.delegate = self; } 然后执行方法并返回NO - (BOOL)gestureRecognize
FirstViewController.m
中,我正在设置interactiveepogesturerecognizer
- (void) viewWillLoad {
// Other stuff..
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
然后执行
方法并返回NO
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return NO;
}
在dealloc中,我将委托设置为零。(我在某个地方读到,在iOS 7中,您必须手动将委托设置为零)
这在FirstViewController
中起作用。但当我将SecondViewController
按到此按钮时,该手势也不起作用。如何仅在FirstViewController中禁用手势
另外,当我弹出FirstViewController
进入RootViewController
并再次尝试推送FirstViewController
时,我会得到对象解除分配错误:
[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280
除了将代理设置为零之外,我还需要做什么?还是我将其设置在错误的位置?在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;
}
我最初把这些答案放在接受答案下面的评论中,但我觉得这需要作为一个答案来表达,以获得更多的关注 通常情况下,你会发现被接受的答案是行不通的。这是因为
视图将出现:
可以在视图添加到导航控制器的视图层次结构之前调用,因此self.navigationController
将为nil
。因此,在某些情况下可能不会禁用InteractiveEPopgestureRecognitor。您最好在viewdide:
中调用它
下面是可以使用的代码(假设您的视图控制器已正确添加到导航控制器的视图层次结构中):
目标-C
敏捷的
只有一种观点,我不知道怎么走。。。但我使用下一个代码完全禁用滑动手势: 在AppDelegate.m中
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
我发现将手势设置为“仅禁用”并不总是有效。它确实有用,但对我来说,它只是在我使用了一次后才起作用。第二次它不会触发回击动作。此外,正如John Rogers所说,使用ViewDidDisplay和ViewWillDisplay作为导航控制器很重要,否则将为零 对我来说,解决方法是委派手势并实现shouldbeagin方法以返回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;
}
我尝试了以上所有方法,但它们对我不起作用。所以我尝试了这个方法,它对我在IOS7和IOS8上都有效。
只需确保您的视图控制器实现此协议,即UIgestureRecognitizerDelegate 并编写下面给出的代码 -(无效)视图将显示:(BOOL)动画{
[super viewWillAppear : animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled =
否;self.navigationController.interactiveEPGEsturerecognizer.delegate= 自我;
} } -(BOOL)手势识别器应开始:(UIGestureRecognizer*)手势识别器{
if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {
return NO;
} else {
return YES;
}
}
这在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
}
添加此代码后,当我尝试做后退手势时,将禁用SecondViewController中所有元素的交互。为什么?Dude
self.navigationController.interactiveepogesturerecognizer.enabled=NO
对我来说很好,请尝试删除代理。啊,很抱歉我忘记了超级,我更新了答案。谢谢,InteractivePostureRecognitizer.enabled
工作正常。但它在我的FirstViewController中不起作用。看来还有其他问题。嗯……你确定你正在实现view>Will吗?你是一个向导,这应该是公认的答案。iOS 12,swift 4.2
[super viewWillAppear : animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled =
if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {
return NO;
} else {
return YES;
}
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
}