前言: 自定義轉場動畫其實並不難, 關鍵在於能夠明白思路, 也就是操作步驟. 本篇博客主要以present轉場動畫為例, 進行分析, 操作, 如有錯誤歡迎簡信與我交流.
不進行修改的話, presentViewController:animated:completion:相信這個方法很多人都是用過, 稱作模態推出界面, 默認都是從屏幕下方推出新的控制器.
自定義的目的就是為了修改固定的推出方式, 同時加上你想要的動畫.
一個關鍵的概念:
UIViewControllerAnimatedTransitioning控制動畫的協議, 需要自己實現這個. 下面來一看看代碼如何進行實現.
@interface ViewController ()
跳轉點擊方法
- (void)click:(UIButton *)sender
{
ModelViewController *modalViewController = [ModelViewController new];
// 指定代理
modalViewController.transitioningDelegate = self;
// Style = 自定義方式
modalViewController.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:modalViewController
animated:YES
completion:NULL];
}
UIViewControllerTransitioningDelegate
// 修改動畫的兩個相關的協議方法, 建議仔細查看UIViewControllerTransitioningDelegate裡面的注釋
-(id)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
// 推出動畫 - 下面會給出代碼 我參照POP動畫Demo寫的.
return [PresentAnimator new];
}
- (id)animationControllerForDismissedController:(UIViewController *)dismissed
{
// 退出
return [DismissAnimator new];
}
// 想要實現手勢控制可在這個方法中進行修改
-(id)interactionControllerForDismissal:(id)animator {
}
關鍵在於動畫如何實現 分別進行的實現PresentAnimator, DismissAnimator的實現.
UIViewControllerAnimatedTransitioning使用這個協議進行修改.
@interface PresentAnimator : NSObject
PresentAnimator.m
// 交互所用時間
- (NSTimeInterval)transitionDuration:(id )transitionContext
{
return 0.5f;
}
// 具體動畫方式, 上面提到的POP
- (void)animateTransition:(id )transitionContext
{
// 這裡拿到即將新推出的VC的View
UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
// 這裡設置退出view具體如何設置
toView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);
toView.center = CGPointMake(transitionContext.containerView.center.x, -transitionContext.containerView.center.y);
[transitionContext.containerView addSubview:toView];
// pop動畫
POPSpringAnimation *positionAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
positionAnim.toValue = @(transitionContext.containerView.center.y);
positionAnim.springBounciness = 10;
[positionAnim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
[transitionContext completeTransition:YES];
}];
POPSpringAnimation *scaleAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnim.springBounciness = 20;
scaleAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(1.2, 1.4)];
[toView.layer pop_addAnimation:positionAnim forKey:@"positionAnimation"];
[toView.layer pop_addAnimation:scaleAnim forKey:@"scaleAnimation"];
}
DismissAnimator.m
- (NSTimeInterval)transitionDuration:(id )transitionContext
{
return 0.5f;
}
- (void)animateTransition:(id )transitionContext
{
// fromVC 很好理解吧?? 就是從哪個控制器推過來回到哪裡去.
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
POPBasicAnimation *offscreenAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
offscreenAnimation.toValue = @(-fromVC.view.layer.position.y);
[offscreenAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
[transitionContext completeTransition:YES];
}];
[fromVC.view.layer pop_addAnimation:offscreenAnimation forKey:@"offscreenAnimation"];
}
順便值得一提的是UINavigationController轉場的動畫也是可以自定義的. 通過這個
UINavigationControllerDelegate來完成
// 簽訂這個協議
self.navigationController.delegate = self;
- (id) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPush) {
// 動畫的方式不變
return [PresentAnimator new];
}else{
return nil;
}
}