iOS閱讀器與直播的控件重疊滑動(dòng)交互詳解
場景一
進(jìn)行一個(gè)閱讀器項(xiàng)目的開發(fā)時(shí),遇到了一個(gè)問題,
需要在點(diǎn)擊綠色區(qū)域時(shí)彈出一個(gè)菜單,因此在該區(qū)域加了一個(gè)View,
然而,當(dāng)在這個(gè)區(qū)域滑動(dòng)時(shí),滑動(dòng)手勢被綠色區(qū)域攔截,手勢無法傳遞到下面的 UIPageViewController 的 View 上
描述
閱讀器上方,搖啊搖,出來一個(gè)綠色的菜單
要求可以點(diǎn),也可以拖動(dòng)
拖動(dòng)是下方 UIPageViewController
的事情。
手勢被綠色視圖擋住了,需要一個(gè)透傳
思路:
把綠色視圖的 hitTest View ,交給正在看的閱讀器,那一頁
這樣拖動(dòng)綠色視圖,也可以滑動(dòng)
class GreenView: UIView{ override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { var afterThat = next while afterThat != nil{ if let tmp = afterThat as? ViewController{ if let target = tmp.pagedController.viewControllers?.first{ return target.view } } else{ afterThat = afterThat?.next } } return nil } }
同時(shí)要捕捉綠色視圖的點(diǎn)擊事件,
通過閱讀頁面的視圖控制器,來捕捉
class ContentCtrl: UIViewController { override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first, greenFrame.contains(touch.location(in: view)){ NotificationCenter.default.post(name: .hitGreen, object: nil) } } }
場景二
用戶在直播室,吃瓜
來了一條重要的消息,必須要用戶處理,
用戶退出直播室,也要展示,
同時(shí)不影響用戶給主播送禮物
如上圖,用戶看到消息,也可以滑動(dòng)閱讀器
( 在消息的區(qū)域,滑動(dòng)無效 )
思路肯定是 window,
脫離控制器,也能展示
實(shí)現(xiàn)一,創(chuàng)建新的 window
class MsgWindow: UIWindow { init() { let originX: CGFloat = 50 let width = UIScreen.main.bounds.width - originX * 2 // 窗口大小固定 super.init(frame: CGRect(x: originX, y: 100, width: width, height: 150)) clipsToBounds = true layer.cornerRadius = 8 backgroundColor = UIColor.cyan // 提升 zIndex windowLevel = UIWindow.Level.statusBar + 100 isHidden = true } func show(){ // 展現(xiàn)的必要配置 if windowScene == nil, let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene{ windowScene = scene } isHidden = false } }
實(shí)現(xiàn) 2,使用老的 window 和 View
class MsgView: UIView { init() { let originX: CGFloat = 50 let width = UIScreen.main.bounds.width - originX * 2 super.init(frame: CGRect(x: originX, y: 100, width: width, height: 150)) clipsToBounds = true layer.cornerRadius = 8 backgroundColor = UIColor.cyan // 設(shè)置 z Index layer.zPosition = CGFloat.infinity isHidden = true } func show(){ // 找到 key window, // 把視圖,添加上去 let scenes = UIApplication.shared.connectedScenes for sce in scenes{ if let windowScene = sce as? UIWindowScene, windowScene.activationState == .foregroundActive , let win = windowScene.windows.first{ isHidden = false win.addSubview(self) return } } } }
場景三
用戶在直播室,吃瓜
來了一條重要的消息,必須要用戶處理,
用戶退出直播室,也要展示,
同時(shí)不影響用戶給主播送禮物
這條消息,很長
( 在消息的區(qū)域,滑動(dòng)有效 )
思路, 擴(kuò)展場景 2 的第 2 種實(shí)現(xiàn)
一句話,限定了響應(yīng)范圍,
重寫了 func point(inside
class MsgView: UIView { let rect : CGRect = { let originX: CGFloat = 50 let width = UIScreen.main.bounds.width - originX * 2 return CGRect(x: originX, y: 100, width: width, height: 400) }() let btnRect = CGRect(x: 10, y: 10, width: 50, height: 50) init() { super.init(frame: rect) clipsToBounds = true layer.cornerRadius = 8 backgroundColor = UIColor.clear layer.zPosition = CGFloat.infinity isHidden = true let bg = UIView(frame: CGRect(origin: .zero, size: rect.size)) bg.backgroundColor = UIColor.cyan bg.alpha = 0.5 addSubview(bg) let btn = UIButton(frame: btnRect) btn.backgroundColor = UIColor.red btn.layer.cornerRadius = 8 btn.backgroundColor = UIColor.white addSubview(btn) btn.addTarget(self, action: #selector(hide), for: .touchUpInside) } @objc func hide(){ isHidden = true } override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { return btnRect.contains(point) } }
為啥這條消息,不用 scroll View ?
ha ha
同時(shí),解決了場景一
一般情況下
到此這篇關(guān)于iOS閱讀器與直播的控件重疊滑動(dòng)交互詳解的文章就介紹到這了,更多相關(guān)iOS重疊滑動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iOS利用Label實(shí)現(xiàn)的簡單高性能標(biāo)簽TagView
這篇文章主要給大家介紹了關(guān)于iOS利用Label實(shí)現(xiàn)的簡單高性能標(biāo)簽TagView的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03IOS開發(fā)OC代碼中創(chuàng)建Swift編寫的視圖控制器
這篇文章主要介紹了IOS開發(fā)OC代碼中創(chuàng)建Swift編寫的視圖控制器的相關(guān)資料,需要的朋友可以參考下2017-06-06iOS中的實(shí)時(shí)遠(yuǎn)程配置全紀(jì)錄
這篇文章主要給大家介紹了關(guān)于iOS中實(shí)時(shí)遠(yuǎn)程配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01iOS指紋驗(yàn)證TouchID應(yīng)用學(xué)習(xí)教程
這篇文章主要為大家詳細(xì)iOS指紋驗(yàn)證TouchID應(yīng)用學(xué)習(xí)教程的第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01