Modal
modal轉(zhuǎn)場(chǎng)方式即使用 presentViewController() 方法推出的方式分唾,默認(rèn)情況下膏潮,第二個(gè)視圖從屏幕下方彈出。下面就來(lái)介紹下 modal 方式轉(zhuǎn)場(chǎng)動(dòng)畫的自定義。
present
還是先來(lái)看一下完成的效果
準(zhǔn)備
- 創(chuàng)建一個(gè)新的工程舰绘,刪掉Main虱而,在AppDelegate中創(chuàng)建自定義UIWindow筏餐,設(shè)置rootVC為ViewController。
- 在ViewController中創(chuàng)建一個(gè)全屏的ImageView牡拇,并且指定一張圖片胖烛;新建一個(gè)按鈕用于present。
- 新建一個(gè)SecondViewController诅迷。同樣創(chuàng)建一個(gè)全屏的ImageView佩番,指定一張和ViewController不同的圖片,新建一個(gè)同于dismiss的按鈕
開始
- 創(chuàng)建一個(gè)文件繼承自 NSObject罢杉,取名為PresentTransition并在.h中遵守 UIViewControllerAnimatedTransitioning協(xié)議趟畏。
- 實(shí)現(xiàn)協(xié)議的兩個(gè)方法,并在其中編寫 Push 的動(dòng)畫滩租。 具體的動(dòng)畫實(shí)現(xiàn)過(guò)程都在代碼的注釋里:
// 返回動(dòng)畫的時(shí)間
- (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;
// 動(dòng)畫
[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];
}];
}
使用動(dòng)畫
- 讓 FirstViewController 遵守 UIViewControllerTransitioningDelegate 協(xié)議赋秀,并將 self.transitioningDelegate 設(shè)置為 self。
self.transitioningDelegate = self;
- 實(shí)現(xiàn) UIViewControllerTransitioningDelegate 協(xié)議的兩個(gè)方法律想,用來(lái)指定動(dòng)畫類猎莲。
// present動(dòng)畫
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
return [[PresentTransition alloc] init];
}
- 在present按鈕的點(diǎn)擊方法中,必須把SecondViewController的transitioningDelegate也設(shè)置為self技即,讓ViewController管理SecondViewController的動(dòng)畫
-(void)presentClick{
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.transitioningDelegate = self; // 必須second同樣設(shè)置delegate才有動(dòng)畫
[self presentViewController:secondVC animated:YES completion:^{
}];
}