gpt4 book ai didi

ios - “弹出”查看动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:30 28 4
gpt4 key购买 nike

我正在尝试找出如何复制此处显示的“弹出式” View 动画: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)
}
}

最佳答案

是的,这似乎是一个自定义转换。您可以通过 UIViewControllerTransitioningDelegateUIViewControllerAnimatedTransitioning

实现此目的

这里有一个很好的入门教程:Raywenderlich: Custom VC Presentation tutorial

一旦您正确理解了委托(delegate)和动画 Controller ,您就可以通过提供的 containerView 编写自己的呈现/关闭动画。

实现此目的的一种方法是通过 UIPresentationController 类上的 frameOfPresentedViewInContainerView 变量简单地指定所需的框架,并通过 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
}
}

...并在呈现和关闭 View 时做一个简单的动画(在 UIViewControllerAnimatedTransitioning 继承类的 containerView 中)以显示“淡入淡出”效果。另请注意,有“n”种方法可以做到这一点,这只是其中一种

关于ios - “弹出”查看动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56967595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com