1.簡單push動畫和present動畫
[self.navigationController.view.layer addAnimation:[self pushAnimation] forKey:@""];
[self.navigationController pushViewController:[] animated:NO];
[self.view.window.layer addAnimation:[self presentAnimation] forKey:nil];
[self presentViewController:second animated:NO completion:nil];
//系統的API
- (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key;
自定義push動畫,然后在push時添加到navigationController中
CATransition -> CAAnimation -> NSObject
CATransition *transition = [CATransition animation];
transition.duration = 0.6;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
//動畫類型,一下是系統API的介紹
/* The name of the transition. Current legal transition types include
* `fade', `moveIn', `push' and `reveal'. Defaults to `fade'. */
transition.type = @"";
/*私有API
cube 立方體效果
pageCurl 向上翻一頁
pageUnCurl 向下翻一頁
rippleEffect 水滴波動效果
suckEffect 變成小布塊飛走的感覺
oglFlip 上下翻轉
cameraIrisHollowClose 相機鏡頭關閉效果
cameraIrisHollowOpen 相機鏡頭打開效果
*/
最后push或present頁面就可以完成動畫
2.非交互式轉場動畫
1.需要當前的navigationController遵循<UINavigationControllerDelegate>并實現其代理方法
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
可以在此代理方法中判斷當前是push還是pop
2.我們需要在代理方法中返回一個UIViewControllerAnimatedTransitioning對象,于是我們創建一個類繼承自NSObject,遵循<UIViewControllerAnimatedTransitioning>協議,并實現其代理方法
代理方法主要有以下2個
// This is used for percent driven interactive transitions, as well as for
// container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
第一個方法將返回動畫的時長
第二個方法將具體實現動畫
//轉場過渡的容器view
UIView *containerView = [transitionContext containerView];
//FromVC
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *fromView = fromViewController.view;
//ToVC
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = toViewController.view;
NSInteger toVCCount = [toViewController.navigationController.viewControllers indexOfObject:toViewController];
NSInteger fromVCCount = [fromViewController.navigationController.viewControllers indexOfObject:fromViewController];
if (toVCCount > fromVCCount) {
//push
[containerView addSubview:fromView];
[containerView addSubview:toView];
}
else{
//pop
[containerView addSubview:toView];
[containerView addSubview:fromView];
}
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
//具體的動畫實現
} completion:^(BOOL finished) {
BOOL wasCancelled = [transitionContext transitionWasCancelled];
//設置transitionContext通知系統動畫執行完畢
[transitionContext completeTransition:!wasCancelled];
}];
實現酷狗的轉場動畫
分別創建兩個類繼承自NSObject,遵循<UIViewControllerAnimatedTransitioning>協議,來實現push和pop的動畫效果
1.push動畫,將將push的view做仿射變換
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;
//轉場過渡的容器view
UIView *containerView = [transitionContext containerView];
//ToVC
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = toViewController.view;
[containerView addSubview:toView];
//將toView繞原點旋轉45度
toView.transform =
2.pop動畫同理
3.交互式轉場動畫
<UINavigationControllerDelegate>實現其代理方法
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController
實現這個方法,系統轉場時,就會知道當前是否有交互式的轉場,有便執行交互轉場,無則執行普通自定義的轉場動畫
UIPercentDrivenInteractiveTransition
類是系統定義的,他遵循UIViewControllerInteractiveTransitioning
協議
此類提供了了以下方法
// These methods should be called by the gesture recognizer or some other logic
// to drive the interaction. This style of interaction controller should only be
// used with an animator that implements a CA style transition in the animator's
// animateTransition: method. If this type of interaction controller is
// specified, the animateTransition: method must ensure to call the
// UIViewControllerTransitionParameters completeTransition: method. The other
// interactive methods on UIViewControllerContextTransitioning should NOT be
// called. If there is an interruptible animator, these methods will either scrub or continue
// the transition in the forward or reverse directions.
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
- (void)cancelInteractiveTransition;
- (void)finishInteractiveTransition;
根據pop時傳遞的手勢信息來計算滑動百分比,從而完成交互動畫和取消動畫