iOS rootViewController定义切换动画

如何切换root时有动画效果呢?

有些需求会让我们比较头疼,某些场景需要切换root而且还要一些动画效果,从而提高用户体验!这个时候不要着急有转场动画这个玩意呢!
转场动画有两种 1.系统提供的专场动画方法 2.自定义专场动画(转场效果比较全面)
  • 1.系统提供的专场动画方法

        ZYTabBarViewController *tabbarController = [[ZYTabBarViewController alloc] init];
        typedef void (^Animation)(void);
        UIWindow* window = self.window;
        tabbarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [UIView transitionWithView:window
                        duration:1
                         options:UIViewAnimationOptionTransitionCrossDissolve
                      animations:^{
                          BOOL oldState = [UIView areAnimationsEnabled];
                          [UIView setAnimationsEnabled:NO];
                          window.rootViewController = tabbarController;
                          [UIView setAnimationsEnabled:oldState];
                      } completion:nil];
    
  • 2.自定义专场动画(该方法不适用于Nav)

          CATransition *animation = [CATransition animation];
          //动画时间
          animation.duration = 0.3f;
          //过滤效果
          animation.type = kCATransitionReveal;
          //枚举值:
          kCATransitionPush 推入效果
          kCATransitionMoveIn 移入效果
          kCATransitionReveal 截开效果
          kCATransitionFade 渐入渐出效果
          //动画执行完毕时是否被移除
          animation.removedOnCompletion = YES;
          //设置方向-该属性从下往上弹出
          animation.subtype = kCATransitionFromBottom;
          枚举值:
          kCATransitionFromRight//右侧弹出
          kCATransitionFromLeft//左侧弹出
          kCATransitionFromTop//顶部弹出
          kCATransitionFromBottom//底部弹出
          [self.window.layer addAnimation:animation forKey:nil];
          self.window.rootViewController = tabbarController;
    
  • 转场动画用type的另一种动画效果

         CATransition *animation = [CATransition animation];
         animation.duration = 1.0;
         //如果想要监听动画的开始和结束有代理方法(最底下)
         animation.delegate = self;
         //设置动画的快慢效果 动画的时间节奏控制
         animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
         枚举值:
         kCAMediaTimingFunctionLinear 匀速
         kCAMediaTimingFunctionEaseIn 慢进
         kCAMediaTimingFunctionEaseOut 慢出
         kCAMediaTimingFunctionEaseInEaseOut 慢进慢出
         kCAMediaTimingFunctionDefault 默认值(慢进慢出)
    
         animation.type =  @"pageCurl"  //向上翻一页
    
         //私有API,注意对于苹果官方没公开的动画类型只能使用字符串,并没有对应的常量定义
         animation.type =  @"cube"  //立方体效果
         animation.type =  @"suckEffect" //收缩效果,如一块布被抽走
         animation.type =  @"oglFlip"  //上下翻转效果
         animation.type =  @"rippleEffect"  //滴水效果
         animation.type =  @"pageCurl"  //向上翻一页
         animation.type =  @"pageUnCurl"  //向下翻一页
         animation.type =  @“cameraIrisHollowOpen”  //摄像头打开效果
         animation.type =  @“cameraIrisHollowClose”  //摄像头关闭效果
    
         //设置动画子类型
         animation.subtype = kCATransitionFromTop;
    
         枚举值:
         kCATransitionFromRight//右侧弹出
         kCATransitionFromLeft//左侧弹出
         kCATransitionFromTop//顶部弹出
         kCATransitionFromBottom//底部弹出
         [self.window.layer addAnimation:animation forKey:nil];
         self.window.rootViewController = tabbarController;
    
          //代理方法
         - (void)animationDidStart:(CAAnimation *)anim{
           动画开始
         }
    
         - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
           动画结束
         }

你可能感兴趣的:(iOS rootViewController定义切换动画)