2

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

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

4

2 回答 2

3

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
于 2018-07-23T05:25:11.077 回答
0

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

来源:https ://stackoverflow.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];
}
于 2021-08-24T03:58:25.960 回答