转场动画的学习
请看简书iOS CAAnimation之CATransition(自定义转场动画)
一、思路
. a跳转b
a: a可以什么都不用做,直接present,
b: b要在init方法里面 写这两个方法,
这个方法保证fromView才不会被移除(及可以在modal后看到a控制器的view)
self.modalPresentationStyle = UIModalPresentationCustom;
这个属性表示在modal、dismiss的时候会走自定义的方法
self.transitioningDelegate = self.animatr;
二、Animatr 方法 && 属性
1. 构造方法
.*这里需要注意,要给定modalPresentationStyle,否则会有坑:请看后面的"坑1"
/** * modalPresentationStyle toVC中设置的转场动画的样式 */+(instancetype)animatrWithModalPresentationStyle: (UIModalPresentationStyle)modalPresentationStyle;/** * modalPresentationStyle toVC中设置的转场动画的样式 */-(instancetype)initWithModalPresentationStyle: (UIModalPresentationStyle)modalPresentationStyle;
.*dismiss & present 动画具体回调方法
//MARK: ---------------------- dismiss & present ------------------------/**dismiss动画*/-(void)dismissAnimaWithBlock: (void(^)(UIViewController*toVC,UIViewController*fromeVC,UIView*toView,UIView*fromeView))dismissAnimaBlock;/**present动画*/-(void)presentAnimaWithBlock: (void(^)(UIViewController*toVC,UIViewController*fromeVC,UIView*toView,UIView*fromeView))presentAnimaBlock;
.*容器视图的view,可以用作遮罩,修改ContainerView的方法
//MARK: ---------------------- setupContainerView -------------------------(void)setupContainerViewWithBlock: (void(^)(UIView *containerView))setupContainerViewBlock;
2. 属性
/**这是属性一定要设置,否则看 上面解释的“坑1”*/@property(nonatomic,assign)UIModalPresentationStylemodalPresentationStyle;//MARK: -------------------- 动画时长 和类型 ------------------------/** present动画时长*/@property(nonatomic,assign)CGFloatpresentDuration;/** dismiss动画时长*/@property(nonatomic,assign)CGFloatdismissDuration;/**动画是否完成,在动画完成时候,一定要把这个属性改为YES*/@property(nonatomic,assign)BOOLisAccomplishAnima;
三、具体实现
注意: 一切都在toVC中设置
设置属性(类延展中相对私有属性)
@interfacePushViewController ()@property(nonatomic,strong) Animatr *animatr;@end
在懒加载中或者viewDidLoad中设置相关属性和实现相关方法
-(void)viewDidLoad { [superviewDidLoad];self.view.backgroundColor = [UIColorblueColor]; [selfsetupAnimatr];//设置Animatr}//设置Animatr-(void)setupAnimatr {//dismiss动画预估时长_animatr.dismissDuration =4;//present动画预估时长_animatr.presentDuration =5;//dismiss转场动画[_animatr dismissAnimaWithBlock:^(UIViewController*toVC,UIViewController*fromeVC,UIView*toView,UIView*fromeView) {NSLog(@"dismiss开始"); [UIViewanimateWithDuration:_animatr.dismissDuration animations:^{ fromeView.frame =CGRectMake(0,0,100,100); } completion:^(BOOLfinished) {//在完成动画的时候一定要把这个属性设置成YES 告诉系统动画完成_animatr.isAccomplishAnima =YES; }]; }];//present转场动画[_animatr presentAnimaWithBlock:^(UIViewController*toVC,UIViewController*fromeVC,UIView*toView,UIView*fromeView) { [UIViewanimateWithDuration:_animatr.presentDuration animations:^{ toView.frame =CGRectMake(0,300,300,300); } completion:^(BOOLfinished) {//在完成动画的时候一定要把这个属性设置成YES 告诉系统动画完成_animatr.isAccomplishAnima =YES; }]; }];//容器视图,装有toView和fromeView,可以作为遮罩[_animatr setupContainerViewWithBlock:^(UIView*containerView) { containerView.backgroundColor = [UIColorcolorWithWhite:0.8alpha:0.8]; }];}