菜鸟教程小白 发表于 2022-12-11 19:45:26

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


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

<p> <a href="/image/4DQtQ.jpg" rel="noreferrer noopener nofollow"><img src="/image/4DQtQ.jpg" alt="enter image description here"/></a> </p>

<p>我知道叠加 View 可以在 appdelegate 中添加到 keywindow,正如 GoogleCast Container 添加的那样:</p>

<pre><code>let appStoryboard = UIStoryboard(name: &#34;Main&#34;, bundle: nil)
let navigationController = appStoryboard.instantiateViewController(withIdentifier: &#34;MainNavigation&#34;)
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()
</code></pre>

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

<p>我忘了提一下,那个应用程序已经启动并运行了。导航栏内大约有 30 个 ViewController </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我会在当前窗口的顶部创建一个窗口,我会在其中放置 radioView 。然后单选 View 将始终位于标准窗口中 View 层次结构的顶部。要允许单选窗口下的标准窗口处理触摸事件,您需要覆盖单选窗口中的 <code>hitTest</code> 以确保它不会处理不应由它处理的事件.</p>

<p>你可以以此为例:</p>

<pre><code>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?) -&gt; UIView? {
      let hitView = super.hitTest(point, with: event)
      if hitView!.isKind(of: PassView.self) {
            return nil
      }
      return hitView
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 所有 Controller 上的快速播放器小部件,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48812883/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48812883/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 所有 Controller 上的快速播放器小部件