OGeek|极客世界-中国程序员成长平台

标题: ios - 所有 Controller 上的快速播放器小部件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:45
标题: ios - 所有 Controller 上的快速播放器小部件

我正在我的应用程序中制作广播播放器。并希望拥有带有信息 + 控制按钮的小部件,在播放时将在应用程序中的所有 Controller 上持续存在。就像在 iTunes 中或像 google 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



Best Answer-推荐答案


我会在当前窗口的顶部创建一个窗口,我会在其中放置 radio View 。然后单选 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4