2016-07-12 61 views
3

我想在代碼中,讓我的視圖在iPad應用程序上鏡像到外部顯示器。在AppDelegate中didFinishLaunching,我有:鏡像視圖控制器到外部顯示器

if ([[UIScreen screens] count] > 1) 
    { 
     UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1]; 
     NSString *availableModeString; 

     for (int i = 0; i < secondScreen.availableModes.count; i++) 
     { 
      availableModeString = [NSString stringWithFormat:@"%f, %f", 
            ((UIScreenMode *)[secondScreen.availableModes objectAtIndex:i]).size.width, 
            ((UIScreenMode *)[secondScreen.availableModes objectAtIndex:i]).size.height]; 

      [[[UIAlertView alloc] initWithTitle:@"Available Mode" message:availableModeString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
      availableModeString = nil; 
     } 

     // undocumented value 3 means no overscan compensation 
     secondScreen.overscanCompensation = 3; 

     self.secondWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 1280, 720)]; 
     self.secondWindow.backgroundColor = [UIColor blueColor]; 
     self.secondWindow.screen = secondScreen; 

     ViewController *viewController = [[ViewController alloc] init]; 
     self.secondWindow.rootViewController = viewController; 

     self.secondWindow.hidden = NO; 
    } 

所有,在外接顯示器顯示出來的是藍色的背景顏色在代碼中設置。

+0

您是在談論使用視頻提供實時Feed還是僅僅在第二個屏幕上模仿視圖? 更重要的是,我們讓第二個屏幕使用觸摸來更改第一個屏幕嗎? – Ariel

+0

大多數情況下,只是將iPad屏幕上的內容反映到Apple TV上,而不是在HD中執行此操作......沒有屏幕上的4個條形圖,當您僅從命令中心執行AirPlay鏡像時會發生這種情況。 – user717452

回答

0

我可以在我的iPhone 6使用鏡像視圖的:

@interface MirrorViewController() 

@property (nonatomic, retain) UIView *viewToMirror; 
@property (nonatomic, retain) UIView *snapshotView; 

@end 

@implementation MirrorViewController 

- (instancetype)initWithViewToMirror:(UIView*)view 
{ 
    self = [super initWithNibName:nil bundle:nil]; 
    if (self == nil) return nil; 
    self.viewToMirror = view; 
    return self; 
} 

- (void)viewDidLoad 
{ 
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)]; 
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; 
} 

- (void)update 
{ 
    [self.snapshotView removeFromSuperview]; 
    self.snapshotView = [self.viewToMirror snapshotViewAfterScreenUpdates:NO]; 
    [self.view addSubview:self.snapshotView]; 
} 

@end 

這裏是你如何能在您的didFinishLaunching代碼中使用它:

if ([[UIScreen screens] count] > 1) 
{ 
    UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1]; 

    // [...] 

    self.secondWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 1280, 720)]; 
    self.secondWindow.backgroundColor = [UIColor blueColor]; 
    self.secondWindow.screen = secondScreen; 

    MirrorViewController *mirrorViewController = [[MirrorViewController alloc] initWithViewToMirror:self.window.rootViewController.view]; 
    self.secondWindow.rootViewController = mirrorViewController; 

    self.secondWindow.hidden = NO; 
} 

我沒有測試它在iPad與外部顯示器,但因爲我沒有裝備。

+0

嗯....我不確定這是應該去的地方,還是應該如何調用?這應該是didLoadView? – user717452

+0

當需要加載UIViewController的視圖時,loadView'由UIKit自動調用。你實現一個UIViewController子類並像我的示例代碼一樣覆蓋loadView。我還沒有測試過,如果使用snabshotView來鏡像這樣的視圖,但如果它的確如此,那麼這個代碼應該是你所需要的:) –

+0

我測試了這個技術並更新了我的答案 –

0

首先檢查應用程序啓動時是否存在外部顯示器。如果第二個顯示器可用,它將爲其創建一個窗口。我在我的應用中使用了以下代碼。

(void)checkForExistingScreenAndInitializeIfPresent 
{ 
    if ([[UIScreen screens] count] > 1) 
    { 
     // Get the screen object that represents the external display. 
     UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1]; 
     // Get the screen's bounds so that you can create a window of the correct size. 
     CGRect screenBounds = secondScreen.bounds; 

     self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds]; 
     self.secondWindow.screen = secondScreen; 

     // Set up initial content to display... 
     // Show the window. 
     self.secondWindow.hidden = NO; 
    } 
} 

通過以下代碼註冊連接和斷開通知。

(void)setUpScreenConnectionNotificationHandlers 
{ 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 

    [center addObserver:self selector:@selector(handleScreenDidConnectNotification:) 
      name:UIScreenDidConnectNotification object:nil]; 
    [center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:) 
      name:UIScreenDidDisconnectNotification object:nil]; 
} 

處理屏幕連接和斷開通知

(void)handleScreenDidConnectNotification:(NSNotification*)aNotification 
{ 
    UIScreen *newScreen = [aNotification object]; 
    CGRect screenBounds = newScreen.bounds; 

    if (!self.secondWindow) 
    { 
     self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds]; 
     self.secondWindow.screen = newScreen; 

     // Set the initial UI for the window. 
    } 
} 

(void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification 
{ 
    if (self.secondWindow) 
    { 
     // Hide and then delete the window. 
     self.secondWindow.hidden = YES; 
     self.secondWindow = nil; 

    } 

} 

如果不創建的一個窗口顯示,或者如果你創建一個窗口,但沒有表現出來,黑色的字段顯示在外部顯示器上。

不能保證所有模式都可以在外部顯示器上使用,因此您不應該依賴於特定模式的可用性。

在極少數情況下,您可能想要爲overscanCompensation屬性使用不同的值,但這樣做總會導致您必須執行的更多工作。例如,如果您使用UIScreenOverscanCompensationInsetBounds,則必須準備好處理非標準顯示大小的界限。

相關問題