如何在 iOS 设备的外部屏幕上显示自定义内容

Posted

技术标签:

【中文标题】如何在 iOS 设备的外部屏幕上显示自定义内容【英文标题】:How To Display Custom Content On A External Screen From A iOS Device 【发布时间】:2018-07-15 07:33:21 【问题描述】:

我在 Objective C 中创建了一个应用程序。我想实现在外部显示器上显示屏幕选定内容区域的功能。例如,我总共有 10 个屏幕,我想显示 4 个屏幕,不想显示整个屏幕,只想在外部显示器中显示选定的视图和屏幕区域。

我已经对此进行了研究,我找到了一个tutorial,但该教程以 swift 语言提供,我想用目标 c 语言实现这些东西。

【问题讨论】:

请检查我的回答。 @SaurabhJain 谢谢 是的,当然.... :) 请看这个问题***.com/questions/51416391/… @SaurabhJain hmm 我已经检查了他的问题,并且我在过去的项目中使用了这个库。在我的项目中只有 2 个 y 轴 :) 【参考方案1】:

Objective C 代码中的 Swift 教程:

#import "ViewController.h"
#import "ExternalScreenViewController.h"

@interface ViewController ()

    UIWindow *externalWindow;
    UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    webView = [[UIWebView alloc] init];
    [self setupScreenNotifications];
    NSURL *url = [NSURL URLWithString:@"http://www.spazstik-software.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [webView loadRequest:req];

    if ([[UIScreen screens] count] > 1) 
        [self setupExternalScreen:[UIScreen screens][1]];
    



- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


-(void)setupScreenNotifications 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidConnect:) name:UIScreenDidConnectNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil];



-(void)externalScreenDidConnect:(NSNotification *)notification 
    UIScreen *screen = (UIScreen *)[notification object];
    if (screen != nil) 
        [self setupExternalScreen:screen];
    


-(void)externalScreenDidDisconnect:(NSNotification *)notification 
    id obj = [notification object];
    if (obj != nil) 
        [self teardownExternalScreen];
    


-(void)setupExternalScreen:(UIScreen *)screen 
    ExternalScreenViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ExternalScreen"];
    externalWindow = [[UIWindow alloc]initWithFrame:screen.bounds];
    externalWindow.rootViewController = vc;
    externalWindow.screen = screen;
    externalWindow.hidden = false;


-(void)teardownExternalScreen 
    if (externalWindow != nil) 
        externalWindow.hidden = true;
        externalWindow = nil;
    

@end

【讨论】:

【参考方案2】:

对于 ios 13 及更高版本,将 setupExternalScreen 方法更改为此

来源:https://***.com/a/61474635/9777391

-(void)setupExternalScreen:(UIScreen *)screen shouldRecurse:(bool)recurse 
    
    UIWindowScene* matchingWindowScene = nil;
    
    // For iOS13 and up find matching UIWindowScene
    for(UIScene *aScene in UIApplication.sharedApplication.connectedScenes)
        UIWindowScene *windowScene = (UIWindowScene*)aScene;
        // Look for UIWindowScene that has matching screen
        if (windowScene.screen == screen) 
            matchingWindowScene = windowScene;
            break;
        
    
    
    if (matchingWindowScene == nil) 
        // UIWindowScene has not been created by iOS rendered yet
        // Lets recall self after delay of two seconds
        if (recurse) 
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^
                [self setupExternalScreen:screen shouldRecurse:NO];
            );
        
        // Dont proceed further if none found
        return;
    
    
    // Instantiate view controller
    ExternalScreenViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ExternalScreen"];
    
    // Setup external window
    externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
    [externalWindow setRootViewController: vc];

    // Set scene
    [externalWindow setWindowScene:matchingWindowScene];
    // Show the window.
    [externalWindow setHidden:NO];

【讨论】:

以上是关于如何在 iOS 设备的外部屏幕上显示自定义内容的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iPad 上运行的 iPhone 应用程序中获取 iOS 8 自定义键盘的主机应用程序屏幕几何图形?

在 iOS 设备的锁屏上添加自定义专辑封面

设备上的 iOS 8 自定义键盘

将 iOS 设备上的触摸发送到另一个 UIScreen(外部显示器)

如何确定自定义 UICollectionViewCell 何时在屏幕上显示为 100%

如何在 iOS 屏幕上点击某个区域时隐藏 UIView 并使其显示