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 }
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 } }