基本用法
1.初始化
CATransition *transition = [CATransition animation];
2.設(shè)置動(dòng)畫時(shí)長(zhǎng),設(shè)置代理人
transition.duration = 1.0f;
transition.delegate = self;
3.設(shè)置切換速度效果
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
枚舉值:
kCAMediaTimingFunctionLinear
kCAMediaTimingFunctionEaseIn
kCAMediaTimingFunctionEaseOut
kCAMediaTimingFunctionEaseInEaseOut
kCAMediaTimingFunctionDefault
4.動(dòng)畫切換風(fēng)格
transition.type = kCATransitionFade;
枚舉值:
kCATransitionFade = 1, // 淡入淡出
kCATransitionPush, // 推進(jìn)效果
kCATransitionReveal, // 揭開(kāi)效果
kCATransitionMoveIn, // 慢慢進(jìn)入并覆蓋效果
5.動(dòng)畫切換方向
transition.subtype = kCATransitionFromTop;//頂部
枚舉值:
kCATransitionFromRight//右側(cè)
kCATransitionFromLeft//左側(cè)
kCATransitionFromTop//頂部
kCATransitionFromBottom//底部
6.進(jìn)行跳轉(zhuǎn)
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController pushViewController:"你要跳轉(zhuǎn)的頁(yè)面" animated:NO];
跳轉(zhuǎn)動(dòng)畫一定設(shè)置為NO,不然會(huì)兩個(gè)效果疊加
注意:addAnimation 添加在何處,誰(shuí)上,是個(gè)難點(diǎn)。
在scrollview滾動(dòng)視圖上的用法:
CATransition *animation = [[CATransition alloc] init];
animation.duration = 1;
animation.type = @"cube";
animation.subtype = kCATransitionFromRight;
[scroll.layer addAnimation:animation forKey:@"ff"];
使用場(chǎng)景二:子控制器view間的轉(zhuǎn)場(chǎng)動(dòng)畫。
- (void)transitionFromViewController:(UIViewController * _Nonnull)fromViewController
toViewController:(UIViewController * _Nonnull)toViewController
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^ _Nullable)(void))animations
completion:(void (^ _Nullable)(BOOL finished))completion
實(shí)例:
[self transitionFromViewController:_third toViewController:_second duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
}];
現(xiàn)將third.view 添加到self.view上,等調(diào)用這個(gè)方法,就會(huì)從third.view 有動(dòng)畫的調(diào)到second.view
present轉(zhuǎn)場(chǎng)動(dòng)畫 使用兩個(gè)協(xié)議,重寫view動(dòng)畫
- <UIViewControllerTransitioningDelegate>
- <UIViewControllerAnimatedTransitioning>
// 1. present 方法
presentedVC.transitionDelegate = self;
[self presentViewController:presentedVC animated:YES completion:nil];
//2. <UIViewControllerAnimatedTransitioning>
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
return 0.5f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
_transitionContext = transitionContext;
_containerView = [transitionContext containerView];
_from = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
_to = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// iOS8之后才有
if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
_fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
_toView = [transitionContext viewForKey:UITransitionContextToViewKey];
} else {
_fromView = _from.view;
_toView = _to.view;
}
}
//3. < UIViewControllerTransitioningDelegate >
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
// 指定的繼承UIViewControllerAnimatedTransitioning協(xié)議的對(duì)象.
// 其中的協(xié)議方法即指定了轉(zhuǎn)場(chǎng)動(dòng)畫.
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
}
push 轉(zhuǎn)場(chǎng)動(dòng)畫 使用兩個(gè)協(xié)議,重寫view動(dòng)畫
- < UINavigationControllerDelegate >
- <UIViewControllerAnimatedTransitioning>
tabbar 轉(zhuǎn)場(chǎng)動(dòng)畫 使用兩個(gè)協(xié)議,重寫view動(dòng)畫
- < UITabBarControllerDelegate >
- <UIViewControllerAnimatedTransitioning>