如何自定义模态视图控制器呈现animation?

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

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

我如何实现它到一个模态视图控制器?

您只需要将转换添加到窗口的图层,并呈现您的控制器而不是推送它:

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

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) 

从苹果开发指南:

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

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

  • 创build你想要呈现的视图控制器。

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

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