轉(zhuǎn)場(chǎng)動(dòng)畫的學(xué)習(xí)
請(qǐng)看簡書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) UIModalPresentationStyle modalPresentationStyle;
//MARK: -------------------- 動(dòng)畫時(shí)長 和類型 ------------------------
/** present動(dòng)畫時(shí)長*/
@property (nonatomic,assign) CGFloat presentDuration;
/** dismiss動(dòng)畫時(shí)長*/
@property (nonatomic,assign) CGFloat dismissDuration;
/**動(dòng)畫是否完成敢靡,在動(dòng)畫完成時(shí)候,一定要把這個(gè)屬性改為YES*/
@property (nonatomic,assign) BOOL isAccomplishAnima;
三苦银、具體實(shí)現(xiàn)
注意 : 一切都在toVC中設(shè)置
- 設(shè)置屬性(類延展中相對(duì)私有屬性)
@interface PushViewController ()
@property (nonatomic,strong) Animatr *animatr;
@end
- 在懶加載中或者
viewDidLoad
中設(shè)置相關(guān)屬性和實(shí)現(xiàn)相關(guān)方法
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
[self setupAnimatr];//設(shè)置Animatr
}
//設(shè)置Animatr
-(void)setupAnimatr {
//dismiss動(dòng)畫預(yù)估時(shí)長
_animatr.dismissDuration = 4;
//present動(dòng)畫預(yù)估時(shí)長
_animatr.presentDuration = 5;
//dismiss轉(zhuǎn)場(chǎng)動(dòng)畫
[_animatr dismissAnimaWithBlock:^(UIViewController *toVC, UIViewController *fromeVC, UIView *toView, UIView *fromeView) {
NSLog(@"dismiss開始");
[UIView animateWithDuration:_animatr.dismissDuration animations:^{
fromeView.frame = CGRectMake(0, 0, 100, 100);
} completion:^(BOOL finished) {
//在完成動(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) {
[UIView animateWithDuration:_animatr.presentDuration animations:^{
toView.frame = CGRectMake(0,300, 300, 300);
} completion:^(BOOL finished) {
//在完成動(dòng)畫的時(shí)候一定要把這個(gè)屬性設(shè)置成YES 告訴系統(tǒng)動(dòng)畫完成
_animatr.isAccomplishAnima = YES;
}];
}];
//容器視圖啸胧,裝有toView和fromeView,可以作為遮罩
[_animatr setupContainerViewWithBlock:^(UIView *containerView) {
containerView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
}];
}
四墓毒、github地址OC版轉(zhuǎn)場(chǎng)動(dòng)畫工具類