SideMenu閱讀
這個(gè)開(kāi)源項(xiàng)目在我的閱讀清單已經(jīng)躺了很久了,剛才看到才發(fā)現(xiàn),已經(jīng)于兩年前停更了
項(xiàng)目的效果是實(shí)現(xiàn)了多樣化的側(cè)邊滑入菜單,具體可以在G站項(xiàng)目首頁(yè)看到。
初始化
ViewDidLoad的時(shí)候有一步setupSideMenu
:
private func setupSideMenu() {
// Define the menus
SideMenuManager.default.leftMenuNavigationController = storyboard?.instantiateViewController(withIdentifier: "LeftMenuNavigationController") as? SideMenuNavigationController
SideMenuManager.default.rightMenuNavigationController = storyboard?.instantiateViewController(withIdentifier: "RightMenuNavigationController") as? SideMenuNavigationController
// Enable gestures. The left and/or right menus must be set up above for these to work.
// Note that these continue to work on the Navigation Controller independent of the View Controller it displays!
SideMenuManager.default.addPanGestureToPresent(toView: navigationController!.navigationBar)
SideMenuManager.default.addScreenEdgePanGesturesToPresent(toView: view)
}
總的來(lái)說(shuō)兩件事:
- 添加左右MenuNavi
- 添加滑動(dòng)手勢(shì)識(shí)別
視圖檢查
可以看到在左側(cè)添加了一個(gè)Navi
源碼檢查
觸發(fā)滑動(dòng)手勢(shì)的入口在handleMenuPan(_ gesture: UIPanGestureRecognizer)
,檢查發(fā)現(xiàn)它是每次通過(guò)menu(forLeftSide: leftSide)
取navi,比如左側(cè)就是leftMenuNavigationController
。這個(gè)leftMenuNavigationController
是我們?cè)趘iewDidLoad的時(shí)候注冊(cè)的。
也就是說(shuō),這個(gè)sideNav是長(zhǎng)期持有的,這一點(diǎn),在對(duì)象檢查里也可以看到
并且,其持有的VC也一直在內(nèi)存里,當(dāng)然,viewDidDisappear肯定是走了的。
UIViewControllerAnimatedTransitioning
要實(shí)現(xiàn)這種基于VC的定制化跳轉(zhuǎn),肯定繞不開(kāi)UIViewControllerTransitioningDelegate
,在SideMenu里,這個(gè)類叫做SideMenuTransitionController
而UIViewControllerAnimatedTransitioning
的實(shí)現(xiàn)類叫做SideMenuAnimationController
Print這個(gè)寫(xiě)法也很有意思
比如cannotPush
這個(gè)case,它利用了Swift支持String Enum的形式,用enum作為錯(cuò)誤類型枚舉,同時(shí)呢,又在里面植入了%@
,在使用的時(shí)候,通過(guò)Print.warning(.cannotPush, arguments: String(describing: potentialNavigationController.self), required: true)
這樣的形式把參數(shù)傳入,實(shí)現(xiàn)一個(gè)非常不錯(cuò)的日志效果。
但是有點(diǎn)遺憾的是,這里沒(méi)有支持參數(shù)檢查比如我增加了無(wú)用參數(shù)或者參數(shù)類型不對(duì),都不能發(fā)出warning,但運(yùn)行時(shí)可能會(huì)因?yàn)槿雲(yún)㈩愋筒黄ヅ浒l(fā)生BAD ACCESS crash
局限
在SideMenu打開(kāi)時(shí)原本頁(yè)面不能滑動(dòng),畢竟它在sideMenuTransitionController(:didPresent:)
里給superVIew添加了一個(gè)點(diǎn)擊和滑動(dòng)手勢(shì)用來(lái)關(guān)閉SideMenu
但是我把這兩個(gè)手勢(shì)注釋掉之后,仍然不能滑動(dòng),原因在于為了防止手勢(shì)觸發(fā)沖突,在彈出時(shí)設(shè)置了isUserInteractionEnabled=false
,這些注釋掉之后,就可以做到彈出時(shí)原本頁(yè)面也可以滑動(dòng)了
檢查發(fā)現(xiàn)這個(gè)星空背景是UITransitionView的,也挺有意思
點(diǎn)贊
總體來(lái)說(shuō)這個(gè)庫(kù)還是寫(xiě)的很不錯(cuò)的,封裝的很棒,支持的效果多,實(shí)現(xiàn)的各種效果也很好,算是一個(gè)很酷的組件
項(xiàng)目源碼