我想标记我的UINavigationController是否动画(推/弹).
我有一个BOOL变量(_isAnimating),下面的代码似乎工作:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { _isAnimating = YES; } - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { _isAnimating = NO; }@H_404_5@但是,它在iOS7中的滑动手势不正确.假设我的导航是:root->视图A – >视图B.我现在在B.
>在滑动开始(从B转到A),调用funcion“navigationController:willShowViewController:animated:(BOOL)animated”,然后_isAnimating = YES.
>正常情况是刷卡完成(返回A),调用函数“navigationController:didShowViewController:animated:(BOOL)animated”,然后_isAnimating = NO.这种情况可以,但是
>如果用户可能只是刷一半(半转换到A),那么不想滑动到上一个视图(查看A),他再次转到当前视图(再次保留B).那么函数“navigationController:didShowViewController:animated:(BOOL)animated”没有被调用,我的变量有不正确的值(_isAnimating = YES).在这种异常情况下,我没有机会更新我的变量.有没有办法更新导航状态?谢谢!
解决方法
解决问题的线索可以在UINavigationController的interactivePopGestureRecognizer属性中找到.这是识别器,通过滑动手势响应弹出控制器.您可以注意到,当用户抬起手指时,识别器的状态更改为UIGestureRecognizerStateEnded.所以,除了导航控制器委托,你应该添加目标到Pop Recognizer:
UIGestureRecognizer *popRecognizer = self.navigationController.interactivePopGestureRecognizer; [popRecognizer addTarget:self action:@selector(navigationControllerPopGestureRecognizerAction:)];@H_404_5@每当Pop Recognizer发生变化,包括手势的结束,这个动作就会被调用.
- (void)navigationControllerPopGestureRecognizerAction:(UIGestureRecognizer *)sender { switch (sender.state) { case UIGestureRecognizerStateEnded: // Next cases are added for relaibility case UIGestureRecognizerStateCancelled: case UIGestureRecognizerStateFailed: _isAnimating = NO; break; default: break; } }@H_404_5@附:不要忘记,从iOS 7开始,interactivePopGestureRecognizer属性可用!