在自定义视图控制器转换中抑制导航推送动画

时间:2013-09-30 22:37:44

标签: iphone cocoa-touch uinavigationcontroller uikit ios7

我正在使用iOS 7中引入的新自定义视图控制器转换以及UINavigationController。我实施了

-navigationController:animationControllerForOperation:fromViewController:toViewController:

在我的UINavigationControllerDelegate中,实施了UIViewControllerAnimatedTransitioning协议,并且自定义转换功能非常出色。

但是我想在推动视图控制器时阻止导航栏中的移动动画。有趣的是,流行动画很好,导航项只是alpha褪色而不是移位。但是推动动画会从右侧移动导航项目。

通过将-pushViewController:animated:替换为-setViewControllers:animated:并将新视图控制器添加到viewControllers数组,我找到了部分解决方案。这会执行正确的alpha淡入淡出动画 - 除了第一次将视图控制器压入堆栈之外。随后的弹出/推动都很好。

知道如何实现这种效果吗? Calendar.app和Photos.app也实现了这一点,因此它应该是可能的(尽管在使用集合视图时它可能是一种特定的行为)。

1 个答案:

答案 0 :(得分:0)

我写了一个解决方法,从导航栏子视图中删除所有位置动画:

@implementation UIView (FTNavigationBar)

static CGFloat leftMargin = 8;

- (void)removePushAnimation {
    CAAnimation *animation = [self.layer animationForKey:@"position"];
    if (animation) {
        if ([animation isKindOfClass:CABasicAnimation.class] && ((CABasicAnimation*)animation).fromValue) {
            CGPoint point = [((CABasicAnimation*)animation).fromValue CGPointValue];
            CGFloat halfSuperWidth = [self superviewOfKind:FTNavigationBar.class].bounds.size.width / 2;
            if ((fabs(point.x - halfSuperWidth) < 2 && fabs(self.center.x - halfSuperWidth) > 2) || fabs(point.x - self.frame.size.width/2 - leftMargin) < 2) {
                self.layer.position = point;
            }
        }
        [self.layer removeAnimationForKey:@"position"];
    }
    for (UIView *subview in [self subviews]) {
        [subview removePushAnimation];
    }
}

@end