gpt4 book ai didi

ios - 为 UIView 创建动画以与 UINavigationController 推送/弹出一致

转载 作者:行者123 更新时间:2023-12-01 16:39:54 39 4
gpt4 key购买 nike

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

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

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

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

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

更新:

我一直在网上搜索这个问题的答案,并尝试调整我最初问题中发布的动画,但似乎无法与导航栏完美匹配,因为它会以动画形式显示在 View 中。任何正确方向的评论或观点将不胜感激。

最佳答案

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

您可以覆盖 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 推送/弹出一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25298105/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com