我的应用程序的初始ViewController具有2个容器视图:第一个包含一个ViewController,在其中包含一个UINavigationController来控制应用程序的主要内容。初始VC的第二个容器视图是一个带有UILabel的小条,它位于导航栏所在的正下方。

主要内容VC的导航堆栈的初始VC隐藏了导航栏。所有后续的VC都将显示导航栏以及初始VC的第二个容器视图。当应用程序离开导航堆栈的初始VC时,我想在第二个容器VC中创建动画。

我的问题是,当导航控制器将VC推入/弹出导航堆栈时,我需要创建什么样的动画来与Navigation Controller执行的动画匹配?我目前正在使用:

[UIView animateWithDuration:0.27f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    self.playerCardVC.frame = secondFrame;
} completion:nil];

但是,当导航栏动画进入视图时,它并不完全匹配。任何想法/想法将不胜感激。

更新:

我一直在网上搜索该问题的答案,并尝试调整在最初问题中发布的动画,但是由于动画在视图中显示,它似乎无法完美匹配导航栏。正确方向的任何评论或要点将不胜感激。

最佳答案

从上面的描述中很难理解您的视图层次结构和导航设计,也许您可​​以发布一些屏幕截图或草图?

您可以覆盖UINavigationController的标准水平推送/弹出动画。为此,您可以定义一个自定义UINavigationControllerDelegate对象和其他一些东西。见下文。

像这样设置您的navController和navControllerDelegate:

UINavigationController *navigationController = [[UINavigationController alloc] init];
self.navigationControllerDelegate = [[NavigationControllerDelegate alloc] init];
navigationController.delegate = self.navigationControllerDelegate;

NavigationControllerDelegate类的位置如下所示:
@interface NavigationControllerDelegate : NSObject <UINavigationControllerDelegate>

@end


#import "NavigationControllerDelegate.h"
#import "CrossFadePushAnimator.h"
#import "CrossFadePopAnimator.h"

@interface NavigationControllerDelegate ()

@property (nonatomic, strong) CrossFadePushAnimator* pushAnimator;
@property (nonatomic, strong) CrossFadePopAnimator* popAnimator;

@end

@implementation NavigationControllerDelegate

- (id)init
{
    self = [super init];
    if (self)
    {
        self.pushAnimator = [CrossFadePushAnimator new];
        self.popAnimator = [CrossFadePopAnimator new];
    }

    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
    {
    if (operation == UINavigationControllerOperationPop)
    {
        return self.popAnimator;
    }
    else if (operation == UINavigationControllerOperationPush)
    {
        return self.pushAnimator;
    }

    return nil;
}

- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
    return nil;
}

pushAnimator看起来像这样:
@interface CrossFadePushAnimator ()

@property (nonatomic, strong) id<UIViewControllerContextTransitioning> transitionContext;

@end

@implementation CrossFadePushAnimator

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return AnimationDuration;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    [[transitionContext containerView] addSubview:toViewController.view];
    toViewController.view.alpha = 1.0f;

    // We are using CAAnimation instead of UIView animation because we need the UIToolbar blur layer to animate
    CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fade.fromValue = @0;
    fade.toValue = @1;
    fade.duration = [self transitionDuration:transitionContext];
    fade.removedOnCompletion = YES;
    fade.delegate = self;

    self.transitionContext = transitionContext;
    [toViewController.view.layer addAnimation:fade forKey:AnimationKey];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
    self.transitionContext = nil;
}

并且popAnimator看起来像pushAnimator。

希望这可以帮助!

关于ios - 为UIView创建动画以使其与UINavigationController push/pop一致,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25298105/

10-13 09:04