使用 UIPresentationController 實現自定義彈窗

amlx8480 7年前發布 | 10K 次閱讀 iOS開發 移動開發

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! :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)

要實現自定義的窗口,只需將上面的 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 即可。

 

來自:http://www.jianshu.com/p/90bb6a2f8122

 

 本文由用戶 amlx8480 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!