自定義轉場動畫其實並不難,關鍵在於把思路——也就是操作步驟——理清楚。本文以 present 轉場為例,示範如何替換系統預設的推出方式,並換上自己想要的動畫。
若不做任何修改,presentViewController:animated:completion: 這個模態推出方法想必大家都很熟悉,預設一律從螢幕下方把新控制器推上來。自定義的目的就是打破這個固定樣式,同時加入自訂動畫。
這裡有個必須掌握的核心概念:UIViewControllerAnimatedTransitioning 協議負責控制動畫,需要自己實作。
@interface ViewController ()@end
- (void)click:(UIButton *)sender {
ModelViewController *modalViewController = [ModelViewController new];
// 指定代理
modalViewController.transitioningDelegate = self;
// Style = 自定義方式
modalViewController.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:modalViewController animated:YES completion:NULL];
}
真正決定動畫的是下面這兩個協議方法,建議仔細閱讀 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 { return nil; }
接下來分別實作 PresentAnimator 與 DismissAnimator,兩者都採用 UIViewControllerAnimatedTransitioning 協議。
@interface PresentAnimator : NSObject@end
PresentAnimator.m:
// 動畫時長 - (NSTimeInterval)transitionDuration:(id)transitionContext { return 0.5f; } // 具體動畫方式,這裡使用 POP - (void)animateTransition:(id )transitionContext { // 取得即將推出的目標 VC 的 view UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].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"]; }
導航控制器的切換動畫同樣可以替換,方法是簽訂並實作 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; } }
把這幾個角色釐清——代理、動畫物件、動畫協議——自定義轉場就不再神祕。