使用 UIPresentationController 实现自定义弹窗

UIPresentationController 是 iOS8 新增的一个 API,可以用它实现自定义的弹窗。但 UIPresentationController 的使用门槛比较高,需要实现几个类和相关代理。Presentr 让这一切变得简单,轻松实现自定义警告窗、菜单或其他任何弹窗。如下图:

Presentr 实现弹窗

Presentr 提供了一个默认的弹窗类 AlertViewController,以下代码即可显示上面的弹窗:

let presenter = Presentr(presentationType: .Alert)
presenter.transitionType = TransitionType.CrossDissolve

let controller = Presentr.alertViewController(title: title, body: body)
let cancelAction = AlertAction(title: "NO, SORRY! 😱", 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)

要实现自定义的窗口,只需将上面的 AlertViewController 换成我们自己的窗口类即可,如下的 SomeViewController。

let alertController = SomeViewController()
customPresentViewController(presenter, viewController: alertController, animated: true, completion: nil)

Presentr 提供了五种显示类型,如下

public enum PresentationType {
  case Alert
  case Popup
  case TopHalf
  case BottomHalf
  case Custom(width: ModalSize, height: ModalSize, center: ModalCenterPosition)
}

通过 PresentationType.Custom 我们可自定义弹窗的大小

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

Presentr 如何实现弹窗

Presentr 封装了 UIPresentationController,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning,类图如下:

首先要清楚两个概念:当前的窗口为 presentingViewController,即将显示的窗口为 presentedViewController。 主要函数调用步骤:

  1. 在主窗口 UIViewController 中调用 customPresentViewController(presenter, viewController: alertController, animated: true, completion: nil)
  2. Presentr:presentationControllerForPresentedViewController,返回PresentrController
  3. Presentr:animationControllerForPresentedController
  4. PresentrController:presentationTransitionWillBegin
  5. PresentrController:frameOfPresentedViewInContainerView
  6. PresentrController:containerViewWillLayoutSubviews

第一步是 Presentr 对 UIPresentationController 细节封装后提供的 UIViewController 的扩展函数。我们只需要写这行代码,剩下的步骤都由 Presentr 完成。这里,Presentr 将设置 PresentedView 的代理 —— transitioningDelegate = self 。

第二步和第三步都是 UIViewControllerTransitioningDelegate 协议的函数,由 Presentr 实现。第二步完成 UIPresentationController 的 子类 PresentrController 的初始化。在初始化创建一个黑色半透明背景视图,如下

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
}

第三步是 PresentedView 出现时的动画效果。苹果自带的动画有从底向上弹窗、渐隐渐现、翻转,Presentr 实现了两个自定义的动画效果:从左往右或从右往左,从上往下。如果需要其他动画效果需要自己实现。

第四步,在 PresentedView 显示之前,添加半透明视图 chromeView 到 PresentrController 的 containerView 中,并添加 chromeView 的显示动画

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
    }
}

第五步,设置 PresentedView 的 frame 大小。

第六步,在布局开始前,将第五步计算的 frame 赋值给 presentedView()!

override func containerViewWillLayoutSubviews() {
    chromeView.frame = containerView!.bounds
    presentedView()!.frame = frameOfPresentedViewInContainerView()
}

这样,我们自定义的弹窗就显示出来了。

使用 UIPresentationController 实现了逻辑的解耦,显示的工作全部交由 UIPresentationController 负责。presentedViewController 不需要提供一个半透明的背景视图,主窗口 presentingViewController 不需要对 presentedView 做额外的处理,只需要调用 present 即可。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 嗯哼嗯哼蹦擦擦~~~ 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 ...
    philiha阅读 5,032评论 0 6
  • 一、做饭是一件快乐的事 最近深圳连续下了几场雨,总算是给这个炎热的夏季带来了一股清凉,尽管深圳的秋天向来短暂,但我...
    雅南的理想国阅读 226,829评论 2 12
  • 如果我身上有标签的话,那应该是,一把岁数,没存款,没相貌,没能力,没谈过恋爱,巨懒还贪吃,所有感兴趣的东西都巧妙的...
    碧莹小主阅读 697评论 0 7
  • 她,颜如月娆。 他,逆天宠她。 他牵着她的手, 对她说,“我属意你为我的皇后……” 她有些不敢相信,直到看见那份诏...
    Aaaaa白阅读 345评论 0 0
  • 这段时间周末都在忙碌新房装修的事,女儿的周末培训、看牙医等都是自己安排或者我父母送去,上海入冬前气候很不稳...
    anniele阅读 249评论 0 0