轉場動畫的學習
一、思路
. 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) UIModalPresentationStyle modalPresentationStyle;
//MARK: -------------------- 動畫時長 和類型 ------------------------
/** present動畫時長*/
@property (nonatomic,assign) CGFloat presentDuration;
/** dismiss動畫時長*/
@property (nonatomic,assign) CGFloat dismissDuration;
/**動畫是否完成,在動畫完成時候,一定要把這個屬性改為YES*/
@property (nonatomic,assign) BOOL isAccomplishAnima;
三、具體實現
注意 : 一切都在toVC中設置
- 設置屬性(類延展中相對私有屬性)
@interface PushViewController ()
@property (nonatomic,strong) Animatr *animatr;
@end
- 在懶加載中或者
viewDidLoad
中設置相關屬性和實現相關方法
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
[self setupAnimatr];//設置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開始");
[UIView animateWithDuration:_animatr.dismissDuration animations:^{
fromeView.frame = CGRectMake(0, 0, 100, 100);
} completion:^(BOOL finished) {
//在完成動畫的時候一定要把這個屬性設置成YES 告訴系統動畫完成
_animatr.isAccomplishAnima = YES;
}];
}];
//present轉場動畫
[_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) {
//在完成動畫的時候一定要把這個屬性設置成YES 告訴系統動畫完成
_animatr.isAccomplishAnima = YES;
}];
}];
//容器視圖,裝有toView和fromeView,可以作為遮罩
[_animatr setupContainerViewWithBlock:^(UIView *containerView) {
containerView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.8];
}];
}
四、github地址OC版轉場動畫工具類