在开发中,我们经常会遇到系统转场动画不适用自己项目的时候,所以需要自定义转场动画来满足我们的需求。一般自定义专场动画是以下三种情况:
- Push
- Modal
- Segue
Push Modal 都是采用的Transition的方式
Push
步骤
- 创建一个继承NSObject并遵守
UIViewControllerAnimatedTransitioning
协议的类 - 1创建的类必须实现以下两个方法:
-(NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
//转场动画时间
-(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
//动画效果 - 在需要调用动画的ViewController必须遵守
UINavigationControllerDelegate
协议,并实现方法 :
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC;
在此方法返回第1步创建的类的实例。
示例
(做一个简单的把push从右往左滑入动画改为类似modal默认的从下往上滑入动画效果)
第一步,创建遵守UIViewControllerAnimatedTransitioning协议的类,一个push的,一个pop的。
Push动画部分
- TDTransitionVerticalPush.h 文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface TDTransitionVerticalPush : NSObject <UIViewControllerAnimatedTransitioning>
@end
- TDTransitionVerticalPush.m 文件
#import "TDTransitionVerticalPush.h"
@implementation TDTransitionVerticalPush
//转场时间
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 0.5;
}
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
// 起始VC
// UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
// 目的VC
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// 转场视图容器
UIView *containerView = [transitionContext containerView];
//动画
toVC.view.frame = CGRectMake(0, Main_Screen_Height, toVC.view.frame.size.width, toVC.view.frame.size.height);
[containerView addSubview:toVC.view];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
toVC.view.frame = CGRectMake(0, 0, toVC.view.frame.size.width, toVC.view.frame.size.height);
} completion:^(BOOL finished) {
BOOL cancelled = transitionContext.transitionWasCancelled;
// 声明过渡结束
[transitionContext completeTransition:!cancelled];
}];
}
Pop动画部分
- TDTransitionVerticalPop.h 文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface TDTransitionVerticalPop : NSObject <UIViewControllerAnimatedTransitioning>
@end
- TDTransitionVerticalPop.m 文件
#import "TDTransitionVerticalPop.h"
@implementation TDTransitionVerticalPush
//转场时间
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 0.5;
}
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
// 起始VC
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
// 目的VC
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// 转场视图容器
UIView *containerView = [transitionContext containerView];
//下降动画
fromVC.view.frame = CGRectMake(0, 0, fromVC.view.frame.size.width, fromVC.view.frame.size.height);
[containerView addSubview:toVC.view];
[containerView addSubview:fromVC.view];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
fromVC.view.frame = CGRectMake(0, Main_Screen_Height, fromVC.view.frame.size.width, fromVC.view.frame.size.height);
} completion:^(BOOL finished) {
BOOL cancelled = transitionContext.transitionWasCancelled;
// 声明过渡结束
[transitionContext completeTransition:!cancelled];
}];
}
第二步,调用动画代理方法实现
(例如有FirstViewController
和SecondViewController
两个ViewController。现在要从FirstViewController
push跳转到SecondViewController
。所以在FirstVeiwController
中处理代理方法)
- FirstViewController.h文件
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#import "TDTransitionVerticalPush.h"
#import "TDTransitionVerticalPop.h"
@interface FirstViewController : UIViewController <UINavigationControllerDelegate>
@end
- FirstViewController.m文件
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 指定navigationController代理
self.navigationController.delegate = self;
}
//实现代理方法,此方法返回nil为默认效果,返回自定义对象则为自定义动画效果
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
//指定特殊ViewController之间的跳转动画
if (operation == UINavigationControllerOperationPush && [toVC isKindOfClass:[SecondViewController class]]) {
return [[TDTransitionVerticalPush alloc]init];
}else if(operation == UINavigationControllerOperationPop && [fromVC isKindOfClass:[SecondViewController class]]){
return [[TDTransitionVerticalPop alloc]init];
}else{
return nil;
}
}
//跳转到SecondViewController
- (IBAction)toSecond:(id)sender {
SecondViewController *vc = [[SecondViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
自定义push动画完成啦
Modal
modal的转场动画自定义步骤跟Push的类似,只是在实现动画时不同。
- 先创建两个遵守
UIViewControllerAnimatedTransitioning
协议的类,一个用于present,一个用于dismiss。实现协议的两个基础方法:
-(NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
和-(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
,自定义动画效果。 - 在需要调用动画的ViewController必须遵守
UIViewControllerTransitioningDelegate
协议。实现
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
和
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
方法。 - 在第一个ViewController的跳转方法中指定第二个ViewController的
transitioningDelegate
属性值为self
.
第三步的代码示例:
- ThirdViewController.m文件
#import <UIKit/UIKit.h>
#import "FourthViewController.h"
#import "TDTransitionHorzontalPresent.h"
#import "TDTransitionHorizontalDismiss.h"
@interface ThridViewController : UIViewController <UIViewControllerTransitioningDelegate>
@end
- ThirdViewController.m文件
#import "ThridViewController.h"
@interface ThridViewController ()
@end
@implementation ThridViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.transitioningDelegate = self;
}
//present动画实现
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
if ([presented isKindOfClass:[FourthViewController class]]) {
return [[TDTransitionHorzontalPresent alloc]init];
}
return nil;
}
//dismiss动画实现
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
if ([dismissed isKindOfClass:[FourthViewController class]]) {
return [[TDTransitionHorizontalDismiss alloc]init];
}
return nil;
}
//storyboard button实现
- (IBAction)modalToFourthVC:(id)sender {
FourthViewController *vc = [[FourthViewController alloc] init];
vc.transitioningDelegate = self;
[self presentViewController: vc animated:YES completion:^{
}];
}
Segue
StoryBoard有它的优势,以后用的越来越多,而且有些逻辑简单的viewcontroller之间 跳转直接连线即可,非常方便。自定义转场动画时,可以选择自定义UIStoryboardSegue子类,或者继续使用跟Push的transition。
先讲在前面,用自定义UIStoryboardSegue子类的式不适用于Push跳转,因为这样会导致NavigationBar消失。自定义UIStoryboardSegue子类推荐用于Modal跳转方式。
自定义UIStoryboard子类的方式这里就不讲了,我讲讲继续使用Transition的这种方式,在Storyboard中使用了Push跳转继续使用这种方式可以完美自定义动画效果,不会出现使用自定义UIStoryboardSegue后navigationbar消失的情况。
- 创建两个继承NSObject并遵守
UIViewControllerAnimatedTransitioning
协议的类,一个用于Push,一个用于Pop。实现协议的两个基础方法自定义动画效果;
-(NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
和
-(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
- 创建一个继承NSObject并遵守
UINavigationControllerDelegate
协议的类。实现协议的方法:
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC;
。 -
在Storyboard文件中的NavigationController添加Object,然后指定此Object的Class为第二步建立的类。
完整的demo在我的Github。