iOS閱讀器與直播的控件重疊滑動交互詳解
場景一
進行一個閱讀器項目的開發(fā)時,遇到了一個問題,
需要在點擊綠色區(qū)域時彈出一個菜單,因此在該區(qū)域加了一個View,
然而,當在這個區(qū)域滑動時,滑動手勢被綠色區(qū)域攔截,手勢無法傳遞到下面的 UIPageViewController 的 View 上

描述
閱讀器上方,搖啊搖,出來一個綠色的菜單
要求可以點,也可以拖動
拖動是下方 UIPageViewController 的事情。
手勢被綠色視圖擋住了,需要一個透傳
思路:
把綠色視圖的 hitTest View ,交給正在看的閱讀器,那一頁
這樣拖動綠色視圖,也可以滑動
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
}
}同時要捕捉綠色視圖的點擊事件,
通過閱讀頁面的視圖控制器,來捕捉
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)
}
}
}場景二
用戶在直播室,吃瓜
來了一條重要的消息,必須要用戶處理,
用戶退出直播室,也要展示,
同時不影響用戶給主播送禮物

如上圖,用戶看到消息,也可以滑動閱讀器
( 在消息的區(qū)域,滑動無效 )
思路肯定是 window,
脫離控制器,也能展示
實現(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
}
}實現(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
}
}
}
}場景三
用戶在直播室,吃瓜
來了一條重要的消息,必須要用戶處理,
用戶退出直播室,也要展示,
同時不影響用戶給主播送禮物
這條消息,很長
( 在消息的區(qū)域,滑動有效 )
思路, 擴展場景 2 的第 2 種實現(xiàn)

一句話,限定了響應范圍,
重寫了 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
同時,解決了場景一
一般情況下
到此這篇關(guān)于iOS閱讀器與直播的控件重疊滑動交互詳解的文章就介紹到這了,更多相關(guān)iOS重疊滑動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iOS利用Label實現(xiàn)的簡單高性能標簽TagView
這篇文章主要給大家介紹了關(guān)于iOS利用Label實現(xiàn)的簡單高性能標簽TagView的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-03-03
IOS開發(fā)OC代碼中創(chuàng)建Swift編寫的視圖控制器
這篇文章主要介紹了IOS開發(fā)OC代碼中創(chuàng)建Swift編寫的視圖控制器的相關(guān)資料,需要的朋友可以參考下2017-06-06

