Skip to content

Instantly share code, notes, and snippets.

@ZhipingYang
Created November 15, 2019 03:25
Show Gist options
  • Save ZhipingYang/83a36c53fef9add0475aab2f8889e6ab to your computer and use it in GitHub Desktop.
Save ZhipingYang/83a36c53fef9add0475aab2f8889e6ab to your computer and use it in GitHub Desktop.
External Display
//
// ScreenMirror.swift
// RoomsController
//
// Created by Daniel Yang on 2019/11/4.
// Copyright © 2019 RingCentral. All rights reserved.
//
import Then
import UIKit
class ScreenMirror: NSObject {
static let share = ScreenMirror()
private var externalWindow: UIWindow?
func start() {
NotificationCenter.default.addObserver(self, selector: #selector(didConnectNotification(_:)), name: UIScreen.didConnectNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didDisconnectNotification(_:)), name: UIScreen.didDisconnectNotification, object: nil)
}
func end() {
NotificationCenter.default.removeObserver(self)
}
@objc func didConnectNotification(_ noti: Notification) {
guard let newScreen = noti.object as? UIScreen else { return }
externalWindow = UIWindow().then {
$0.frame = newScreen.bounds
$0.rootViewController = GameViewController()
$0.screen = newScreen
$0.isHidden = false
}
(UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = TapViewController()
}
@objc func didDisconnectNotification(_ noti: Notification) {
externalWindow?.isHidden = true
externalWindow = nil
(UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = GameViewController()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment