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。 主要函数调用步骤:
- 在主窗口 UIViewController 中调用
customPresentViewController(presenter, viewController: alertController, animated: true, completion: nil)
- Presentr:
presentationControllerForPresentedViewController
,返回PresentrController
- Presentr:
animationControllerForPresentedController
- PresentrController:
presentationTransitionWillBegin
- PresentrController:
frameOfPresentedViewInContainerView
- 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 即可。
网友评论