gpt4 book ai didi

ios - 所有 Controller 上的快速播放器小部件

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:41 29 4
gpt4 key购买 nike

我正在我的应用程序中播放广播。并希望拥有带有信息 + 控制按钮的小部件,这些小部件将在播放时持续存在于应用程序中的所有 Controller 上。就像在 iTunes 中或像谷歌 chromecast 容器一样,它将从底部推送其他 View Controller 的所有元素(不是覆盖元素)

enter image description here

我知道覆盖 View 可以在 appdelegate 中添加到 keywindow,正如 GoogleCast Container 添加的那样:

let appStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = appStoryboard.instantiateViewController(withIdentifier: "MainNavigation")
let castContainerVC: GCKUICastContainerViewController = GCKCastContext.sharedInstance().createCastContainerController(for: navigationController)
castContainerVC.miniMediaControlsItemEnabled = true
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = castContainerVC
self.window?.makeKeyAndVisible()

但我不明白,我如何设计和实例化我的 Controller +将其添加到窗口+在没有播放时隐藏。

我忘了说,那个应用程序已经启动并运行了。导航栏内大约有 30 个 View Controller

最佳答案

我会在当前窗口的顶部创建一个窗口,我会在其中放置 radio View 。然后 radio View 将始终位于标准窗口中 View 层次结构的顶部。要允许单选窗口下的标准窗口处理触摸事件,您需要覆盖单选窗口中的 hitTest 以确保它不会处理不应由它处理的事件.

你可以以此为例:

import UIKit

class RadioController: UIViewController {

fileprivate struct Static {
static let window: UIHigherWindow = {
let window = UIHigherWindow(frame: UIScreen.main.bounds)
window.rootViewController = RadioController()
window.windowLevel = UIWindowLevelAlert - 1
return window
}()
}

override func loadView() {
// use passView, as it will pass the touch events tu underlying window
self.view = PassView()
self.view.backgroundColor = UIColor.clear
}

override func viewDidLoad() {
super.viewDidLoad()

// configure whatever subview you want, in your case the radio view
let radioView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 50, width: UIScreen.main.bounds.width, height: 50))
radioView.backgroundColor = .red
self.view.addSubview(radioView)
}

override var preferredStatusBarStyle: UIStatusBarStyle {
// this will now define the status bar style, as it will become the topmost window for most of the time
return .default
}

static var showing: Bool {
get {
return !Static.window.isHidden
}
}

static func present() {
Static.window.isHidden = false
}

static func hide() {
Static.window.isHidden = true
}
}

// MARK: Passing touch events to the back view
class PassView: UIView {}

class UIHigherWindow: UIWindow {

// this will allow touch events to be processed by the default window
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
if hitView!.isKind(of: PassView.self) {
return nil
}
return hitView
}
}

关于ios - 所有 Controller 上的快速播放器小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812883/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com