学习笔记-使用Airplay镜像实现多屏显示

简介

AirPlay是通过Wi-Fi连接支持AirPlay的设备,然后使用镜像功能就能在其他设备显示内容,播放声音。今天我要分享的是使用Airplay之后,我们要在电脑上显示和iOS设备上显示的内容不一样,简言之iOS设备类似于一个遥控器。
配合demo看感觉要好一些哇:https://github.com/Xcc1994/AirplayTestDemo

原理

获取新的屏幕信息--->创建一个新的Window--->将新的Window对应的Screen屏幕设置为新的屏幕--->设置新的屏幕的UI显示

实现

 //连接Airplay Screen
    func connectScreen() {
        if UIScreen.screens.count > 1 {
            UIScreen.screens.forEach({[weak self] (screen:UIScreen) in
                if screen != UIScreen.main {
                    self?.didConnectScreen(screen: screen)
                }
            })
        }
    }
 //开始Airplay监听
    func beginReceivingScreenNotification() {
        //首先检查是否已经连接了
        connectScreen()
        //启动监听
        NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidConnect, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveDisConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)
    }

每次我们创建一个工程都可以在AppDelegate文件中看见一个window,一般情况下我们可以不用理会它。screen我们拿来获取一些屏幕信息之外也不会做其它处理。但是现在不一样了,我们要实现多屏显示,从需求也可以知道会多一个window。
我们启动程序的时候先去判断现在screen的数量,未连接AirPlay进行投屏的时候,我们的screen只有一个main screen。一旦连接AirPlay进行投屏时遍历现在的screen,除去main screen之外,我们还可以获取在电脑上显示的screen。
(启动监听的两个通知后面再介绍……)

extension AirplayService:AirplayScreenDelegate {
    //已经连接
    @objc func didConnectScreen(screen: UIScreen) {
        if currentViewController == nil {
            let defalutScreen =  DefalutViewController()
            currentViewController = defalutScreen
        }
        if screenWindow == nil {
            let window = UIWindow(frame: screen.bounds)
            screenWindow = window
            screenWindow?.rootViewController = currentViewController
            screenWindow?.isHidden = false
        }
        screenStatus = .Connected
        screenWindow?.screen = screen
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: AirplayConstants.Notifications.ScreenDidConnected), object: nil)


    }
    //断开连接
    @objc func didDisconnectScrren(screen: UIScreen) {
        screenStatus = .Disconnected
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: AirplayConstants.Notifications.ScreenDidDisconnected), object: nil)
    }
}

一旦我们获取到screen之后,我们就可以将新的Window对应的Screen屏幕设置为新的屏幕。如果投屏的window当前的currentViewController为nil ,我进行了一个小处理写了一个类似于屏保的默认DefalutViewController。
现在我们来说说之前启动监听时发送的通知:

 //启动监听
 NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidConnect, object: nil)
 NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveDisConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)
    @objc func didReceiveConnectScreenNotification(noti:NSNotification) {
        let screen:UIScreen = noti.object as! UIScreen
        self.didConnectScreen(screen: screen)
    }
    
    @objc func didReceiveDisConnectScreenNotification(noti:NSNotification) {
        let screen:UIScreen = noti.object as! UIScreen
        self.didDisconnectScrren(screen: screen)
    }
    

我们连接AirPlay进行投屏,可以有2种手顺:
1、先连接AirPlay进行投屏,再启动App君,这样的话,我们启动App君的时候获取screen的时候就可以获取到多个screen,排除开main screen之外就可以进行连接。
2、先启动App君,再连接AirPlay进行投屏。或者是运行App君的过程中遇到奥特曼打小怪兽断开了连接之后,世界和平之后又连接上的情况,我们就需要通过系统的两个通知来解决问题了。

   @discardableResult func updateViewController(viewController:AirplayViewController,animation:Bool) -> Bool{
        guard screenWindow != nil else {
            return false
        }
        currentViewController?.airplayViewWillClose()
        currentViewController = viewController
        screenWindow?.rootViewController?.removeFromParentViewController()
        screenWindow?.rootViewController?.navigationController?.removeFromParentViewController()
        screenWindow?.rootViewController = nil
        screenWindow?.rootViewController = currentViewController
        currentViewController?.view.frame = (screenWindow?.bounds)!
        currentViewController?.view.layoutIfNeeded()
        currentViewController?.airplayViewWillShow()
        if animation {
            let maskView:UIView = UIView(frame: (currentViewController?.view.frame)!)
            maskView.backgroundColor = UIColor.black
            self.currentViewController?.view.addSubview(maskView)
            UIView.animate(withDuration: 0.5, animations: { 
                maskView.alpha = 0
            }, completion: { (finish:Bool) in
                maskView.removeFromSuperview()
                
            })
        }
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: AirplayConstants.Notifications.ViewControllerDidUpdate), object: nil)
        return true
    }

这个是自己写的一个更新电脑端Window的ViewController的一个动画。很简单就不说了。我是用Xcode8.1建的项目,用swift3.0的语法写的,@discardableResult声明是告诉编译器此方法可以不用接收返回值。

测试

1、(真机)使用AirServer软件实现AirPlay,一定要打开镜像

学习笔记-使用Airplay镜像实现多屏显示_第1张图片
FullSizeRender.jpg

2、(模拟器)

学习笔记-使用Airplay镜像实现多屏显示_第2张图片
屏幕快照 2017-01-13 12.56.17.png

测试妹子催我改bug……可怜的程序员。。空了再写……
(有错误的地方欢迎提出来)
参考资料: http://blog.csdn.net/songrotek/article/details/8949442

你可能感兴趣的:(学习笔记-使用Airplay镜像实现多屏显示)