最近模仿潮汐,發(fā)現(xiàn)不會實現(xiàn)自定義轉(zhuǎn)場動畫,就學(xué)習(xí)了下,還是有點不是很清晰,寫下感想反思
View Controller Transition(控制器過渡)
每個 View Controller 是一個 Scene,View Controller Transition 便是從一個 Scene 轉(zhuǎn)換到另外一個 Scene.
model轉(zhuǎn)場方式僅限于modalPresentationStyle屬性為 UIModalPresentationFullScreen 或 UIModalPresentationCustom 這兩種模式
如果是試圖控制器A跳轉(zhuǎn)至B
自定義跳轉(zhuǎn)需要三個東西:
1.轉(zhuǎn)場代理
A作為代理,遵守UIViewControllerTransitioningDelegate協(xié)議
2.動畫控制器(Animation Controller):
負(fù)責(zé)添加視圖以及執(zhí)行動畫. 可創(chuàng)建兩個遵守UIViewControllerAnimatedTransitioning的對象C,D,實現(xiàn)A到B和B到A的動畫效果
3.轉(zhuǎn)場環(huán)境
由UIKit提供
對象A實現(xiàn)UIViewControllerTransitioningDelegate 的兩個方法:
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController*)presented presentingController:(UIViewController*)presenting sourceController ///返回對象C
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController*)dismissed //返回對象D
對象C.D實現(xiàn)UIViewControllerAnimatedTransitioning 協(xié)議的兩個方法:
- (NSTimeInterval)transitionDuration:(id<UIViewControllerAnimatedTransitioning>)transitionContext
返回動畫執(zhí)行時間
- (void)animateTransition:(id<UIViewControllerAnimatedTransitioning>)transitionContext
從A到B,或者從B到A的動畫效果
例:對象C中主要代碼(負(fù)責(zé)A到B):
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
// 1. Get controllers from transition context
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// 2. Set init frame for toVC
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
toVC.view.frame = CGRectOffset(finalFrame, 0, screenBounds.size.height);
// 3. Add toVC's view to containerView
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toVC.view];
// 4. Do animate now
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:duration
delay:0.0
usingSpringWithDamping:0.6
initialSpringVelocity:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
toVC.view.frame = finalFrame;
} completion:^(BOOL finished) {
// 5. Tell context that we completed.
[transitionContext completeTransition:YES];
}];
}
這是我學(xué)習(xí)的demo:https://github.com/onevcat/VCTransitionDemo
更詳細(xì)的可參見http://blog.devtang.com/2016/03/13/iOS-transition-guide/