使用 UIPresentationController 實(shí)現(xiàn)自定義彈窗

UIPresentationController 是 iOS8 新增的一個(gè) API,可以用它實(shí)現(xiàn)自定義的彈窗。但 UIPresentationController 的使用門檻比較高,需要實(shí)現(xiàn)幾個(gè)類和相關(guān)代理。Presentr 讓這一切變得簡單,輕松實(shí)現(xiàn)自定義警告窗、菜單或其他任何彈窗。如下圖:

Presentr 實(shí)現(xiàn)彈窗

Presentr 提供了一個(gè)默認(rèn)的彈窗類 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)

要實(shí)現(xiàn)自定義的窗口,只需將上面的 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 如何實(shí)現(xiàn)彈窗

Presentr 封裝了 UIPresentationController,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning,類圖如下:

首先要清楚兩個(gè)概念:當(dāng)前的窗口為 presentingViewController,即將顯示的窗口為 presentedViewController。 主要函數(shù)調(diào)用步驟:

  1. 在主窗口 UIViewController 中調(diào)用 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 對(duì) UIPresentationController 細(xì)節(jié)封裝后提供的 UIViewController 的擴(kuò)展函數(shù)。我們只需要寫這行代碼,剩下的步驟都由 Presentr 完成。這里,Presentr 將設(shè)置 PresentedView 的代理 —— transitioningDelegate = self 。

第二步和第三步都是 UIViewControllerTransitioningDelegate 協(xié)議的函數(shù),由 Presentr 實(shí)現(xiàn)。第二步完成 UIPresentationController 的 子類 PresentrController 的初始化。在初始化創(chuàng)建一個(gè)黑色半透明背景視圖,如下

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 出現(xiàn)時(shí)的動(dòng)畫效果。蘋果自帶的動(dòng)畫有從底向上彈窗、漸隱漸現(xiàn)、翻轉(zhuǎn),Presentr 實(shí)現(xiàn)了兩個(gè)自定義的動(dòng)畫效果:從左往右或從右往左,從上往下。如果需要其他動(dòng)畫效果需要自己實(shí)現(xiàn)。

第四步,在 PresentedView 顯示之前,添加半透明視圖 chromeView 到 PresentrController 的 containerView 中,并添加 chromeView 的顯示動(dòng)畫

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

第五步,設(shè)置 PresentedView 的 frame 大小。

第六步,在布局開始前,將第五步計(jì)算的 frame 賦值給 presentedView()!

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

這樣,我們自定義的彈窗就顯示出來了。

使用 UIPresentationController 實(shí)現(xiàn)了邏輯的解耦,顯示的工作全部交由 UIPresentationController 負(fù)責(zé)。presentedViewController 不需要提供一個(gè)半透明的背景視圖,主窗口 presentingViewController 不需要對(duì) presentedView 做額外的處理,只需要調(diào)用 present 即可。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 嗯哼嗯哼蹦擦擦~~~ 轉(zhuǎn)載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 ...
    philiha閱讀 4,988評(píng)論 0 6
  • 一、做飯是一件快樂的事 最近深圳連續(xù)下了幾場雨,總算是給這個(gè)炎熱的夏季帶來了一股清涼,盡管深圳的秋天向來短暫,但我...
    雅南的理想國閱讀 226,786評(píng)論 2 12
  • 如果我身上有標(biāo)簽的話,那應(yīng)該是,一把歲數(shù),沒存款,沒相貌,沒能力,沒談過戀愛,巨懶還貪吃,所有感興趣的東西都巧妙的...
    碧瑩小主閱讀 688評(píng)論 0 7
  • 她,顏如月嬈。 他,逆天寵她。 他牽著她的手, 對(duì)她說,“我屬意你為我的皇后……” 她有些不敢相信,直到看見那份詔...
    Aaaaa白閱讀 335評(píng)論 0 0
  • 這段時(shí)間周末都在忙碌新房裝修的事,女兒的周末培訓(xùn)、看牙醫(yī)等都是自己安排或者我父母送去,上海入冬前氣候很不穩(wěn)...
    anniele閱讀 249評(píng)論 0 0