彈出房間界面
功能說明
- 區分彈出秀場&普通房間
- 如果用戶是秀場直播,那麼彈出秀場房間界面
- 如果用戶是電腦直播,那麼彈出普通房間界面
- 秀場界面彈出
- 直接以Model方式,從底部彈出即可
- 普通界面彈出
- 將界面PUSH出來
- 注意:
- 1> 界面push之後,不能再顯示UITabbar(默認依然在下面)
- 2> 彈出的界面需要因此導航欄,但是首頁導航欄依然正常展示
- 3> 彈出完成後,實現全屏Pop,手勢左滑退出控制器
效果展示
- 效果展示
彈出功能實現
- 監聽UICollectionView的Cell點擊
- 在BaseAnchorViewController中實現對應的代碼方法監聽即可
- 創建兩個房間的控制器(房間不同,需要創建兩個控制器)
- RoomNormalViewController(普通房間控制器)
- RoomShowViewController(秀場房間控制器)
- 在監聽Cell點擊方法中執行彈出邏輯
- 取出對應的AnchorModel對象
- 根據anchor中的isVertical屬性,判斷彈出方式
- 0 : 電腦直播
- 1 : 手機直播
- 根據方式,彈出控制器即可
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// 1.取出主播信息
let anchor = baseVM.anchorGroups[indexPath.section].anchors[indexPath.item]
// 2.判斷是手機直播&電腦直播
anchor.isVertical == 1 ? presentShowLiveVM(anchor: anchor) : pushNormalLiveVM(anchor: anchor)
}
private func presentShowLiveVM(anchor : AnchorModel) {
// 創建控制器
let showVC = RoomShowViewController()
// 彈出
present(showVC, animated: true, completion: nil)
}
private func pushNormalLiveVM(anchor : AnchorModel) {
// 1.創建控制器
let normalVC = RoomNormalViewController()
// 2.push
navigationController?.pushViewController(normalVC, animated: true)
}
顯示&隱藏UINavigationBar
- 顯示&隱藏UINavigationBar使用下面方法即可
- navigationController.setNavigationBarHidden(true, animated: true)
- true : 隱藏
- false : 顯示
- 在RoomNormalViewController的系統回調方法中分別執行對應的方法
- viewWillAppear:執行隱藏方法
- viewWillDisappear:執行顯示方法
- 注意:
- 這樣做會產生一個問題,就是原來系統的左邊緣的滑動退出控制器手勢無效了
- 一旦我們隱藏了導航欄,那麼該手勢就會自動失效
- 如果希望繼續顯示,則需要按照下面的步驟
- 1> navigationController?.interactivePopGestureRecognizer?.delegate = self
- 2> navigationController?.interactivePopGestureRecognizer?.isEnabled = true
- 代碼如下:
class RoomNormalViewController: UIViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.purple
// 保留Pop手勢
navigationController?.interactivePopGestureRecognizer?.delegate = self
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
}
隱藏UITabbar
- 在push時隱藏UITabbar非常簡答
- 因爲系統已經提供了一個控制器的屬性
- 設置控制器的hidesBottomBarWhenPushed爲true即可
- 注意:所有的push控制器其實都需要因此,那麼怎麼辦呢?
- 方案一:抽取統一的父類
- 方案二:截取push過程,在push的那一刻,設置控制器的屬性
- 該位置採取方案二:更爲簡單
- 如何截取?
- 自定義UINavigationController
- 重寫
pushViewController(_ viewController: UIViewController, animated: Bool)
方法 - 將之前的UINavigationController改成自定義的
- 代碼如下:
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
viewController.hidesBottomBarWhenPushed = true
super.pushViewController(viewController, animated: animated)
}
}
添加全屏Pop手勢
思路分析:添加全屏Pop手勢一直以來都有兩種實現思路
- 方式一:自己在Push出來的View中添加UIPanGestureRecognizer手勢
- 添加手勢,監聽手勢滑動
- 隨着手勢滑動,逐漸退出控制器的View
- 優點:最容易想到,使用自定義專場即可實現
- 缺點:較爲麻煩
- 方式二:利用運行時機制,獲取系統的Pop手勢target&action
- 獲取系統的手勢監聽View
- 獲取系統的手勢target&action
- 創建自己的手勢,添加事件監聽時,使用上步中的target&action
- 將手勢,添加到系統手勢監聽的View中
- 優點:實現非常簡單
- 缺點:需要用到運行時機制,且不容易想到
實現方案:方案二(簡單)
- 該位置我們採用第二種實現方案,也是我個人常用的實現方案
- 首先,我們已經知道系統是有一個左滑手勢
- 該左滑手勢只能在左邊緣滑動纔會生效
- 但是該手勢的View&target&action系統已經創建好了
- 我們可以自己創建一個手勢,但是利用系統的View&target&action
- 問題?
- 1> 如果獲取系統手勢的View?
- 比如簡單,因爲可以直接獲取interactivePopGestureRecognizer手勢
- interactivePopGestureRecognizer.view即可獲得
- 2> 如果獲取target&action
- 該方式較爲麻煩,需要使用KVC
- 通過某一些Key來獲取可接
- 3> 通過哪些key呢?
- 需要用運行時,遍歷所有的屬性找到
- 需要用運行時,遍歷所有的屬性找到
- 1> 如果獲取系統手勢的View?
- 代碼分析:
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// 1.取出手勢&view
guard let gesture = interactivePopGestureRecognizer else { return }
gesture.isEnabled = false
let gestureView = gesture.view
// 2.獲取所有的target
let target = (gesture.value(forKey: "_targets") as? [NSObject])?.first
guard let transition = target?.value(forKey: "_target") else { return }
let action = Selector(("handleNavigationTransition:"))
// 3.創建新的手勢
let popGes = UIPanGestureRecognizer()
popGes.maximumNumberOfTouches = 1
gestureView?.addGestureRecognizer(popGes)
popGes.addTarget(transition, action: action)
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
viewController.hidesBottomBarWhenPushed = true
super.pushViewController(viewController, animated: animated)
}
}