多窗口IOS App

IOS设备是可以接外接屏幕的。如果想在两个屏幕上显示不同的内容,你需要2个窗口和两个viewController。

设置第二个屏幕视图的函数:

    func setupScreen(screen: UIScreen){
        if self.secondWindow != nil {
            return
        }
        self.secondWindow = UIWindow(frame: screen.bounds)
        self.secondWindow?.screen = screen
        self.secondWindow?.hidden = false
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyBoard.instantiateViewControllerWithIdentifier("secondWindowVC") as UIViewController
        self.secondWindow?.rootViewController = viewController
    }

在APP刚启动时,首先要设置监听,监听屏幕接入和断开这两个动作,分别执行设置第二个屏幕和删除第二个屏幕的操作。还有,如果在应用启动前已经连接了第二屏幕,就直接设置第二个屏幕。

        let notification = NSNotificationCenter.defaultCenter()
        notification.addObserver(self, selector: Selector("screenDidConnect:"), name: UIScreenDidConnectNotification, object: nil)
        notification.addObserver(self, selector: Selector("screenDidDisconnect"), name: UIScreenDidDisconnectNotification, object: nil)
        //如果刚一启动时就连接了屏幕,就开始设置屏幕
        if UIScreen.screens().count >= 2 {
            let secondScreen = UIScreen.screens()[1] as UIScreen
            self.setupScreen(secondScreen)
        }
在连接屏幕时:

    func screenDidConnect(notification: NSNotification){
        let screen = notification.object as! UIScreen
        self.setupScreen(screen)
    }

断开屏幕时:

    func screenDidDisconnect(notification: NSNotification){
        let screen = notification.object as! UIScreen
        if self.secondWindow?.screen == screen {
            self.secondWindow = nil
        }
    }




你可能感兴趣的:(多窗口IOS App)