CATransition

基本用法
1.初始化
CATransition  *transition = [CATransition animation];
2.设置动画时长,设置代理人
transition.duration = 1.0f;
transition.delegate = self;
3.设置切换速度效果
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
枚举值:
kCAMediaTimingFunctionLinear
kCAMediaTimingFunctionEaseIn
kCAMediaTimingFunctionEaseOut
kCAMediaTimingFunctionEaseInEaseOut
kCAMediaTimingFunctionDefault
4.动画切换风格
transition.type = kCATransitionFade;
枚举值:
kCATransitionFade = 1,     // 淡入淡出
kCATransitionPush,         // 推进效果
kCATransitionReveal,       // 揭开效果     
kCATransitionMoveIn,       // 慢慢进入并覆盖效果
5.动画切换方向
transition.subtype = kCATransitionFromTop;//顶部
枚举值:
kCATransitionFromRight//右侧
kCATransitionFromLeft//左侧
kCATransitionFromTop//顶部
kCATransitionFromBottom//底部
6.进行跳转
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController pushViewController:"你要跳转的页面" animated:NO];
跳转动画一定设置为NO,不然会两个效果叠加

注意:addAnimation 添加在何处,谁上,是个难点。

在scrollview滚动视图上的用法:

    CATransition *animation = [[CATransition alloc] init];
    animation.duration = 1;
    animation.type = @"cube";
    animation.subtype = kCATransitionFromRight;
    [scroll.layer addAnimation:animation forKey:@"ff"];
使用场景二:子控制器view间的转场动画。

- (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

实例:
[self transitionFromViewController:_third toViewController:_second duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        
    } completion:^(BOOL finished) {
        
    }];
现将third.view 添加到self.view上,等调用这个方法,就会从third.view 有动画的调到second.view
present转场动画 使用两个协议,重写view动画

参考:git
参考:

// 1. present 方法
presentedVC.transitionDelegate = self;
[self presentViewController:presentedVC animated:YES completion:nil];

//2. 

- (NSTimeInterval)transitionDuration:(nullable id )transitionContext {
    return 0.5f;
}

- (void)animateTransition:(id )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 )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    // 指定的继承UIViewControllerAnimatedTransitioning协议的对象.
    // 其中的协议方法即指定了转场动画.
}

- (nullable id )animationControllerForDismissedController:(UIViewController *)dismissed {
   
}

push 转场动画 使用两个协议,重写view动画

参考:

  • < UINavigationControllerDelegate >
tabbar 转场动画 使用两个协议,重写view动画
  • < UITabBarControllerDelegate >

你可能感兴趣的:(CATransition)