轉(zhuǎn)場(chǎng)動(dòng)畫的學(xué)習(xí)
請(qǐng)看簡(jiǎn)書iOS CAAnimation之CATransition(自定義轉(zhuǎn)場(chǎng)動(dòng)畫)
一、思路
. a跳轉(zhuǎn)b
a: a可以什么都不用做,直接present,
b: b要在init方法里面 寫這兩個(gè)方法,
這個(gè)方法保證fromView才不會(huì)被移除(及可以在modal后看到a控制器的view)
self.modalPresentationStyle = UIModalPresentationCustom;
這個(gè)屬性表示在modal、dismiss的時(shí)候會(huì)走自定義的方法
self.transitioningDelegate = self.animatr;
二、Animatr 方法 && 屬性
1. 構(gòu)造方法
.*這里需要注意,要給定modalPresentationStyle,否則會(huì)有坑:請(qǐng)看后面的"坑1"
/** * modalPresentationStyle toVC中設(shè)置的轉(zhuǎn)場(chǎng)動(dòng)畫的樣式 */+(instancetype)animatrWithModalPresentationStyle: (UIModalPresentationStyle)modalPresentationStyle;/** * modalPresentationStyle toVC中設(shè)置的轉(zhuǎn)場(chǎng)動(dòng)畫的樣式 */-(instancetype)initWithModalPresentationStyle: (UIModalPresentationStyle)modalPresentationStyle;
.*dismiss & present 動(dòng)畫具體回調(diào)方法
//MARK: ---------------------- dismiss & present ------------------------/**dismiss動(dòng)畫*/-(void)dismissAnimaWithBlock: (void(^)(UIViewController*toVC,UIViewController*fromeVC,UIView*toView,UIView*fromeView))dismissAnimaBlock;/**present動(dòng)畫*/-(void)presentAnimaWithBlock: (void(^)(UIViewController*toVC,UIViewController*fromeVC,UIView*toView,UIView*fromeView))presentAnimaBlock;
.*容器視圖的view,可以用作遮罩,修改ContainerView的方法
//MARK: ---------------------- setupContainerView -------------------------(void)setupContainerViewWithBlock: (void(^)(UIView *containerView))setupContainerViewBlock;
2. 屬性
/**這是屬性一定要設(shè)置,否則看 上面解釋的“坑1”*/@property(nonatomic,assign)UIModalPresentationStylemodalPresentationStyle;//MARK:? -------------------- 動(dòng)畫時(shí)長(zhǎng) 和類型 ------------------------/** present動(dòng)畫時(shí)長(zhǎng)*/@property(nonatomic,assign)CGFloatpresentDuration;/** dismiss動(dòng)畫時(shí)長(zhǎng)*/@property(nonatomic,assign)CGFloatdismissDuration;/**動(dòng)畫是否完成,在動(dòng)畫完成時(shí)候,一定要把這個(gè)屬性改為YES*/@property(nonatomic,assign)BOOLisAccomplishAnima;
三、具體實(shí)現(xiàn)
注意: 一切都在toVC中設(shè)置
設(shè)置屬性(類延展中相對(duì)私有屬性)
@interfacePushViewController ()@property(nonatomic,strong) Animatr *animatr;@end
在懶加載中或者viewDidLoad中設(shè)置相關(guān)屬性和實(shí)現(xiàn)相關(guān)方法
-(void)viewDidLoad { [superviewDidLoad];self.view.backgroundColor = [UIColorblueColor]; [selfsetupAnimatr];//設(shè)置Animatr}//設(shè)置Animatr-(void)setupAnimatr {//dismiss動(dòng)畫預(yù)估時(shí)長(zhǎng)_animatr.dismissDuration =4;//present動(dòng)畫預(yù)估時(shí)長(zhǎng)_animatr.presentDuration =5;//dismiss轉(zhuǎn)場(chǎng)動(dòng)畫[_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) {//在完成動(dòng)畫的時(shí)候一定要把這個(gè)屬性設(shè)置成YES 告訴系統(tǒng)動(dòng)畫完成_animatr.isAccomplishAnima =YES;? ? }]; }];//present轉(zhuǎn)場(chǎng)動(dòng)畫[_animatr presentAnimaWithBlock:^(UIViewController*toVC,UIViewController*fromeVC,UIView*toView,UIView*fromeView) {? ? [UIViewanimateWithDuration:_animatr.presentDuration animations:^{? ? ? ? toView.frame =CGRectMake(0,300,300,300);? ? } completion:^(BOOLfinished) {//在完成動(dòng)畫的時(shí)候一定要把這個(gè)屬性設(shè)置成YES 告訴系統(tǒng)動(dòng)畫完成_animatr.isAccomplishAnima =YES;? ? }]; }];//容器視圖,裝有toView和fromeView,可以作為遮罩[_animatr setupContainerViewWithBlock:^(UIView*containerView) {? ? containerView.backgroundColor = [UIColorcolorWithWhite:0.8alpha:0.8]; }];}