我正在尝试找出如何复制此处显示的“弹出”视图动画:https://imgur.com/a/irFqdiP。我正在使用此当前代码来显示我的viewController,但目前仅使用淡入淡出动画。如何创建显示的动画?这是风俗吗?
let popup : SearchViewController = self.storyboard?.instantiateViewController(withIdentifier: "SearchViewController") as! SearchViewController
self.presentOnRoot(with: popup)
extension UIViewController {
func presentOnRoot(`with` viewController : UIViewController){
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(navigationController, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
是的,这似乎是自定义转换。您可以通过UIViewControllerTransitioningDelegate
和UIViewControllerAnimatedTransitioning
这是一个很好的教程,始于:Raywenderlich: Custom VC Presentation tutorial
一旦正确理解了委托和动画控制器,就可以通过提供的containerView
编写自己的当前/关闭动画。
执行此操作的一种方法是,通过frameOfPresentedViewInContainerView
类上的UIPresentationController
变量简单地指定所需的帧,并通过containerView对其显示和退出进行动画处理。这是我正在谈论的presentationController的示例:-
class YourPresentationController: UIPresentationController {
//Do whatever stuff you want to. I've added a view with black tint to emphasise presentation
let dimmingView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
return view
}()
override func presentationTransitionWillBegin() {
dimmingView.frame = containerView!.bounds
dimmingView.alpha = 0.0
containerView!.insertSubview(dimmingView, at: 0)
presentedViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
self.dimmingView.alpha = 1.0
})
}
override func dismissalTransitionWillBegin() {
presentedViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
self.dimmingView.alpha = 0.0
}) { _ in
self.dimmingView.removeFromSuperview()
}
}
//This variable is what I'm talking about. Just specify the frame that you would want your presented VC's view to be at:-
override var frameOfPresentedViewInContainerView: CGRect {
var size = containerView!.bounds.size
return CGRect(origin: CGPoint(x: 0, y: 0), size: size)
}
override func containerViewWillLayoutSubviews() {
dimmingView.frame = containerView!.bounds
presentedView?.frame = frameOfPresentedViewInContainerView
}
}
...,并在呈现和消除视图(在您的containerView
继承类的UIViewControllerAnimatedTransitioning
中)时做一个简单的动画,以显示那种“淡入淡出”的效果。还请注意,有“ n”种方式可以做到这一点,而这只是其中的一种