如何实现pushViewController:animated:的不同页面转换特效?


 
 
1. 首先要明确的是,不使用pushViewController的默认动画,所以在调用这个函数时,要将animated设置为NO.
2. 使用普通的来CATransition实现转换效果,代码如下:

CATransition *animation = [CATransition animation];

[animation setDuration:0.3];

[animation setType: kCATransitionMoveIn];

[animation setSubtype: kCATransitionFromTop];

[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    

    [self.navigationController pushViewController:m_poseAddIssueViewController animated:NO];

    

    [self.navigationController.view.layer addAnimation:animation forKey:nill];


#import "TPNavigationController.h" #import "TPContainerViewController.h" #import "TPContainerNavigationController.h" #import "UIViewController+TPNavigationExtension.h" #import <TPFoundation/TPUIUtils.h> @implementation TPContainerNavigationController + (instancetype)wrapNavigationControllerWithViewController:(UIViewController *)viewController { return [[self alloc] initWithViewController:viewController]; } - (instancetype)initWithViewController:(UIViewController *)viewController { if (self = [super init]) { self.viewControllers = @[viewController]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // 禁用包装的vc的返回手势 self.interactivePopGestureRecognizer.enabled = NO; } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { TPContainerViewController *fromContainer = (TPContainerViewController *)self.navigationController.topViewController; if (![self shouldKeepOrientationFromController:[fromContainer rootViewController] toController:viewController]) { [self ensureOrientationIsPortrait]; } // push一个包装后的控制器 TPContainerViewController *wrapViewController = [TPContainerViewController wrapViewController:viewController]; if (self.tpNavigationController.viewControllers.count >= 1) { if (@available(iOS 9.0, *)) { wrapViewController.hidesBottomBarWhenPushed = YES; viewController.hidesBottomBarWhenPushed = YES; } else { // for iOS 8, set tabbar hidden [[self tabBarController].tabBar setHidden:YES]; } } //使用Xcode12.0.1编译的App,在iOS14上运行时,当调用popToRootViewControllerAnimated:YES到vc栈中非相邻的vc时,会发现rootViewController的tabbar消失了,如果是popToRootViewControllerAnimated:NO则没问题 // 应该是官方的bug,先复写该方法解决 else { if (@available(iOS 14.0, *)) { viewController.hidesBottomBarWhenPushed = NO; } } [self.navigationController pushViewController:wrapViewController animated:animated]; } - (UIViewController *)popViewControllerAnimated:(BOOL)animated { NSInteger controllersCount = self.navigationController.viewControllers.count; if (controllersCount > 1) { TPContainerViewController *fromContainer = (TPContainerViewController *)self.navigationController.topViewController; TPContainerViewController *toContainer = self.navigationController.viewControllers[controllersCount - 2]; if (![self shouldKeepOrientationFromController:[fromContainer rootViewController] toController:[toContainer rootViewController]]) { [self ensureOrientationIsPortrait]; } } if (self.navigationController) { return [self.navigationController popViewControllerAnimated:animated]; } return [super popViewControllerAnimated:animated]; } - (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated { [self ensureOrientationIsPortrait]; return [self.navigationController popToRootViewControllerAnimated:animated]; } - (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated { [self ensureOrientationIsPortrait]; TPNavigationController *tpNavigationController = viewController.tpNavigationController; // pop到指定vc时应找到对应的tpContainerViewController NSInteger index = [tpNavigationController.tpChildViewControllers indexOfObject:viewController]; return [self.navigationController popToViewController:tpNavigationController.viewControllers[index] animated:animated]; } - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion { [self ensureOrientationIsPortrait]; [self.navigationController presentViewController:viewControllerToPresent animated:flag completion:completion]; } - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion { [self ensureOrientationIsPortrait]; [self.navigationController dismissViewControllerAnimated:flag completion:completion]; } - (void)setDelegate:(id<UINavigationControllerDelegate>)delegate { self.navigationController.delegate = delegate; } - (void)ensureOrientationIsPortrait { if (UIInterfaceOrientationIsPortrait([TPUIUtils getScreenOrientation])) { return; } if (@available(iOS 16.0, *)) { [self setNeedsUpdateOfSupportedInterfaceOrientations]; } [TPUIUtils changeScreenOrientationTo:UIInterfaceOrientationPortrait]; } - (BOOL)shouldKeepOrientationFromController:(UIViewController *)fromController toController:(UIViewController *)toController { SEL selector = @selector(controllersNeedToKeepOrientationWhenNavigating); if ([fromController respondsToSelector:selector]) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" NSArray *controllers = [fromController performSelector:selector]; #pragma clang diagnostic pop NSString *toControllerName = NSStringFromClass([toController class]); if ([controllers containsObject:toControllerName]) { return YES; } } return NO; } @end
09-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值