如何自定义Modal View Controller呈现动画?

时间:2013-11-12 14:22:49

标签: ios core-animation modalviewcontroller

我想添加一个CAAnimation(或其他东西),而不是设置uiviewcontroller的modalTransitionStyle。此代码可以在navigationController中执行自定义动画

CATransition* transition = [CATransition animation];
          transition.duration = 0.4;
          transition.type = kCATransitionFade;
          transition.subtype = kCATransitionFromBottom;
          [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
          [self.navigationController pushViewController:adjustViewController animated:NO];

如何将其实现到模态视图控制器?

3 个答案:

答案 0 :(得分:56)

您只需要将过渡添加到窗口的图层,然后显示您的控制器而不是推送它:

     CATransition* transition = [CATransition animation];
     transition.duration = 1;
     transition.type = kCATransitionFade;
     transition.subtype = kCATransitionFromBottom;
     [self.view.window.layer addAnimation:transition forKey:kCATransition];
     [self presentViewController:adjustViewController animated:NO completion:nil];

答案 1 :(得分:6)

Swift版本

let viewController = YourViewController()
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionFade
transition.subtype = kCATransitionFromBottom
view.window!.layer.add(transition, forKey: kCATransition)
present(viewController, animated: false, completion: nil)

答案 2 :(得分:5)

来自苹果开发指南:

Presenting a View Controller Using Custom Animations

要使用自定义动画显示视图控制器,请在现有视图控制器的操作方法中执行以下操作:

  • 创建要显示的视图控制器。

  • 创建自定义转换委托对象并将其分配给视图控制器的transitioningDelegate属性。转换委托的方法应该在被问到时创建并返回自定义动画对象。

  • 调用presentViewController:animated:completion:方法来显示视图控制器。

...