我正在尝试使用代码,在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 :(得分: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上测试它,因为我没有这个装备。
答案 1 :(得分: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
,则必须准备好处理非标准显示大小的范围。