Modal
modal轉場方式即使用 presentViewController() 方法推出的方式,默認情況下,第二個視圖從屏幕下方彈出。下面就來介紹下 modal 方式轉場動畫的自定義。
present
還是先來看一下完成的效果
準備
- 創建一個新的工程,刪掉Main,在AppDelegate中創建自定義UIWindow,設置rootVC為ViewController。
- 在ViewController中創建一個全屏的ImageView,并且指定一張圖片;新建一個按鈕用于present。
- 新建一個SecondViewController。同樣創建一個全屏的ImageView,指定一張和ViewController不同的圖片,新建一個同于dismiss的按鈕
開始
- 創建一個文件繼承自 NSObject,取名為PresentTransition并在.h中遵守 UIViewControllerAnimatedTransitioning協議。
- 實現協議的兩個方法,并在其中編寫 Push 的動畫。 具體的動畫實現過程都在代碼的注釋里:
// 返回動畫的時間
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 0.8;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
ViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
SecondViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView * container = [transitionContext containerView];
[container addSubview:toVC.view];
[container bringSubviewToFront:fromVC.view];
// 改變m34
CATransform3D transfrom = CATransform3DIdentity;
transfrom.m34 = -0.002;
container.layer.sublayerTransform = transfrom;
// 設置archPoint和position
CGRect initalFrame = [transitionContext initialFrameForViewController:fromVC];
toVC.view.frame = initalFrame;
fromVC.view.frame = initalFrame;
fromVC.view.layer.anchorPoint = CGPointMake(0, 0.5);
fromVC.view.layer.position = CGPointMake(0, initalFrame.size.height / 2.0);
// 添加陰影效果
CAGradientLayer * shadowLayer = [[CAGradientLayer alloc] init];
shadowLayer.colors =@[
[UIColor colorWithWhite:0 alpha:1],
[UIColor colorWithWhite:0 alpha:0.5],
[UIColor colorWithWhite:1 alpha:0.5]
];
shadowLayer.startPoint = CGPointMake(0, 0.5);
shadowLayer.endPoint = CGPointMake(1, 0.5);
shadowLayer.frame = initalFrame;
UIView * shadow = [[UIView alloc] initWithFrame:initalFrame];
shadow.backgroundColor = [UIColor clearColor];
[shadow.layer addSublayer:shadowLayer];
[fromVC.view addSubview:shadow];
shadow.alpha = 0;
// 動畫
[UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:2 animations:^{
fromVC.view.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
shadow.alpha = 1.0;
} completion:^(BOOL finished) {
fromVC.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
fromVC.view.layer.position = CGPointMake(CGRectGetMidX(initalFrame), CGRectGetMidY(initalFrame));
fromVC.view.layer.transform = CATransform3DIdentity;
[shadow removeFromSuperview];
[transitionContext completeTransition:YES];
}];
}
使用動畫
- 讓 FirstViewController 遵守 UIViewControllerTransitioningDelegate 協議,并將 self.transitioningDelegate 設置為 self。
self.transitioningDelegate = self;
- 實現 UIViewControllerTransitioningDelegate 協議的兩個方法,用來指定動畫類。
// present動畫
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
return [[PresentTransition alloc] init];
}
- 在present按鈕的點擊方法中,必須把SecondViewController的transitioningDelegate也設置為self,讓ViewController管理SecondViewController的動畫
-(void)presentClick{
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.transitioningDelegate = self; // 必須second同樣設置delegate才有動畫
[self presentViewController:secondVC animated:YES completion:^{
}];
}
自定義Present動畫完成