ios 如何自定义模态视图控制器呈现动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19931710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:52:29  来源:igfitidea点击:

How to custom Modal View Controller presenting animation?

ioscore-animationmodalviewcontroller

提问by leavez

Instead of setting uiviewcontroller's modalTransitionStyle, I want to add an CAAnimation(or some other thing). This code can perform an custom animation in navigationController

modalTransitionStyle我想添加一个 CAAnimation(或其他一些东西),而不是设置 uiviewcontroller's 。此代码可以在 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];

How can I implement it to a Modal View Controller?

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

回答by rdelmar

You just need to add the transition to the window's layer, and present your controller rather than pushing it:

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

     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];

回答by Umit Kaya

Swift Version

迅捷版

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)

回答by liaogang

From apple develop guide:

来自苹果开发指南:

Presenting a View Controller Using Custom Animations

使用自定义动画呈现视图控制器

To present a view controller using custom animations, do the following in an action method of your existing view controllers:

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

  • Create the view controller that you want to present.

  • Create your custom transitioning delegate object and assign it to the view controller's transitioningDelegate property. The methods of your transitioning delegate should create and return your custom animator objects when asked.

  • Call the presentViewController:animated:completion: method to present the view controller.

  • 创建要呈现的视图控制器。

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

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

...

...