Modal
modal轉(zhuǎn)場方式即使用 presentViewController() 方法推出的方式,默認情況下懂讯,第二個視圖從屏幕下方彈出刻获。下面就來介紹下 modal 方式轉(zhuǎn)場動畫的自定義坪创。
present
還是先來看一下完成的效果
準備
- 創(chuàng)建一個新的工程,刪掉Main侍瑟,在AppDelegate中創(chuàng)建自定義UIWindow唐片,設(shè)置rootVC為ViewController。
- 在ViewController中創(chuàng)建一個全屏的ImageView涨颜,并且指定一張圖片费韭;新建一個按鈕用于present。
- 新建一個SecondViewController庭瑰。同樣創(chuàng)建一個全屏的ImageView星持,指定一張和ViewController不同的圖片,新建一個同于dismiss的按鈕
開始
- 創(chuàng)建一個文件繼承自 NSObject弹灭,取名為PresentTransition并在.h中遵守 UIViewControllerAnimatedTransitioning協(xié)議督暂。
- 實現(xiàn)協(xié)議的兩個方法揪垄,并在其中編寫 Push 的動畫。 具體的動畫實現(xiàn)過程都在代碼的注釋里:
// 返回動畫的時間
- (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;
// 設(shè)置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 協(xié)議逻翁,并將 self.transitioningDelegate 設(shè)置為 self饥努。
self.transitioningDelegate = self;
- 實現(xiàn) UIViewControllerTransitioningDelegate 協(xié)議的兩個方法,用來指定動畫類八回。
// present動畫
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
return [[PresentTransition alloc] init];
}
- 在present按鈕的點擊方法中酷愧,必須把SecondViewController的transitioningDelegate也設(shè)置為self,讓ViewController管理SecondViewController的動畫
-(void)presentClick{
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.transitioningDelegate = self; // 必須second同樣設(shè)置delegate才有動畫
[self presentViewController:secondVC animated:YES completion:^{
}];
}
自定義Present動畫完成