使用 UIPresentationController 实现自定义弹窗
amlx8480
8年前
<p>UIPresentationController 是 iOS8 新增的一个 API,可以用它实现自定义的弹窗。但 UIPresentationController 的使用门槛比较高,需要实现几个类和相关代理。 Presentr 让这一切变得简单,轻松实现自定义警告窗、菜单或其他任何弹窗。如下图:</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/27896a8f445da53fd3aea140a918029c.png"></p> <h3>Presentr 实现弹窗</h3> <p>Presentr 提供了一个默认的弹窗类 AlertViewController,以下代码即可显示上面的弹窗:</p> <pre> <code class="language-objectivec">let presenter = Presentr(presentationType: .Alert) presenter.transitionType = TransitionType.CrossDissolve let controller = Presentr.alertViewController(title: title, body: body) let cancelAction = AlertAction(title: "NO, SORRY! :scream:", style: .Cancel) { alert in print("CANCEL!!") } let okAction = AlertAction(title: "DO IT! ��", style: .Destructive) { alert in print("OK!!") } alertController.addAction(cancelAction) alertController.addAction(okAction) customPresentViewController(presenter, viewController: controller, animated: true, completion: nil)</code></pre> <p>要实现自定义的窗口,只需将上面的 AlertViewController 换成我们自己的窗口类即可,如下的 SomeViewController。</p> <pre> <code class="language-objectivec">let alertController = SomeViewController() customPresentViewController(presenter, viewController: alertController, animated: true, completion: nil)</code></pre> <p>Presentr 提供了五种显示类型,如下</p> <pre> <code class="language-objectivec">public enum PresentationType { case Alert case Popup case TopHalf case BottomHalf case Custom(width: ModalSize, height: ModalSize, center: ModalCenterPosition) }</code></pre> <p>通过 PresentationType.Custom 我们可自定义弹窗的大小</p> <pre> <code class="language-objectivec">let width = ModalSize.Custom(size: 320) let height = ModalSize.Custom(size: 150) let center = ModalCenterPosition.Center //CustomOrigin(origin: CGPoint(x: 0, y: 100)) let customType = PresentationType.Custom(width: width, height: height, center: center) let customPresenter = Presentr(presentationType: customType) customPresenter.transitionType = .CrossDissolve</code></pre> <h3>Presentr 如何实现弹窗</h3> <p>Presentr 封装了 UIPresentationController,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning,类图如下:</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/eaac3ee6ddb56e64a6c4b2b629b25b54.png"></p> <p>首先要清楚两个概念:当前的窗口为 presentingViewController,即将显示的窗口为 presentedViewController。 主要函数调用步骤:</p> <ol> <li>在主窗口 UIViewController 中调用 customPresentViewController(presenter, viewController: alertController, animated: true, completion: nil)</li> <li>Presentr: presentationControllerForPresentedViewController ,返回 PresentrController</li> <li>Presentr: animationControllerForPresentedController</li> <li>PresentrController: presentationTransitionWillBegin</li> <li>PresentrController: frameOfPresentedViewInContainerView</li> <li>PresentrController: containerViewWillLayoutSubviews</li> </ol> <p>第一步是 Presentr 对 UIPresentationController 细节封装后提供的 UIViewController 的扩展函数。我们只需要写这行代码,剩下的步骤都由 Presentr 完成。这里,Presentr 将设置 PresentedView 的代理 —— transitioningDelegate = self 。</p> <p>第二步和第三步都是 UIViewControllerTransitioningDelegate 协议的函数,由 Presentr 实现。第二步完成 UIPresentationController 的 子类 PresentrController 的初始化。在初始化创建一个黑色半透明背景视图,如下</p> <pre> <code class="language-objectivec">init(presentedViewController: UIViewController, presentingViewController: UIViewController, presentationType: PresentationType, roundCorners: Bool, dismissOnTap: Bool) { self.presentationType = presentationType self.roundCorners = roundCorners self.dismissOnTap = dismissOnTap super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController) setupChromeView() } private func setupChromeView() { let tap = UITapGestureRecognizer(target: self, action: #selector(chromeViewTapped)) chromeView.addGestureRecognizer(tap) chromeView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7) chromeView.alpha = 0 }</code></pre> <p>第三步是 PresentedView 出现时的动画效果。苹果自带的动画有从底向上弹窗、渐隐渐现、翻转,Presentr 实现了两个自定义的动画效果:从左往右或从右往左,从上往下。如果需要其他动画效果需要自己实现。</p> <p>第四步,在 PresentedView 显示之前,添加半透明视图 chromeView 到 PresentrController 的 containerView 中,并添加 chromeView 的显示动画</p> <pre> <code class="language-objectivec">override func presentationTransitionWillBegin() { chromeView.frame = containerView!.bounds chromeView.alpha = 0.0 containerView?.insertSubview(chromeView, atIndex: 0) if let coordinator = presentedViewController.transitionCoordinator() { coordinator.animateAlongsideTransition({ context in self.chromeView.alpha = 1.0 }, completion: nil) } else { chromeView.alpha = 1.0 } }</code></pre> <p>第五步,设置 PresentedView 的 frame 大小。</p> <p>第六步,在布局开始前,将第五步计算的 frame 赋值给 presentedView()!</p> <pre> <code class="language-objectivec">override func containerViewWillLayoutSubviews() { chromeView.frame = containerView!.bounds presentedView()!.frame = frameOfPresentedViewInContainerView() }</code></pre> <p>这样,我们自定义的弹窗就显示出来了。</p> <p>使用 UIPresentationController 实现了逻辑的解耦,显示的工作全部交由 UIPresentationController 负责。presentedViewController 不需要提供一个半透明的背景视图,主窗口 presentingViewController 不需要对 presentedView 做额外的处理,只需要调用 present 即可。</p> <p> </p> <p>来自:http://www.jianshu.com/p/90bb6a2f8122</p> <p> </p>