containerView
这个只读属性是被呈现的视图所在的view,我们可以在这个view上添加其他视图,通过
insertSubview(dummyView, atIndex: 0)这个方法。
接下来就用到两个代理 UIViewControllerTransitioningDelegate和 UIViewControllerAnimatedTransitioning
实现代理方法,isPresented是一个是否被呈现的标记
// 返回负责转场的控制器对象 func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? { return PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting) } // 返回提供modal动画的对象 func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresented = true return self } // 返回提供dismiss动画的对象 func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { isPresented = false return self } // 动画时长 func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval { return 1.2 } // 转场动画实现函数 func animateTransition(transitionContext: UIViewControllerContextTransitioning) { }
在最后一个方法中实现转场动画的过程。 此外,还要设置
// 设置转场动画代理 vc.transitioningDelegate = self // 将转场动画设置为自定义 vc.modalPresentationStyle = UIModalPresentationStyle.Custom
否则,转场动画是无法自定义的。