2

I'm creating custom controls for an MPMoviePlayerController, so I'm having to manually add my own AirPlay button via MPVolumeView.

Whenever I fade in my custom movie player controls, I start a timer that will fade them out after a certain number of seconds. But before I fade them out, I'd like to check if the AirPlay popover view is visible - and if so, delay the fade out until the popover view has disappeared. How might I do this?

AirPlay UIPopoverView

How can I programatically determine if the AirPlay popover view is visible?

4

2 回答 2

3

首先,公共 API 中没有允许您执行此操作的方法。

如果您确实想尝试,我能想到的一种方法是观察您的应用程序的主窗口,并留意何时将弹出视图添加到视图层次结构中。通过进一步观察这个弹出框,您应该能够知道它何时被关闭。

然而,这真的很 hacky,因为它依赖于不改变的底层实现(在未来的 iOS 版本中它可以而且不可避免地会改变)。

于 2013-10-06T00:33:28.793 回答
1

在 lxt 的指导下,我设法把一些东西放在一起。这并不理想,但它似乎对我有用。

我在 airplay 按钮上添加了一个轻击手势,以了解何时开始观察关键窗口。当它被点击时,我开始观察。

- (void)airplayTapped:(UITapGestureRecognizer *)gesture {
    NSLog(@"airplay added");
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    if (!keyWindow) {
        keyWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }

    // this occurs before the airplay popover view is added, so this is the 
    // number we want to check for in observeValueForKeyPath: to determine  
    // if the view has been dismissed
    self.windowSubviews = keyWindow.layer.sublayers.count; 

    [keyWindow addObserver:self forKeyPath:@"layer.sublayers" options:NSKeyValueObservingOptionNew context:NULL];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (![keyPath isEqualToString:@"layer.sublayers"]) {
        return;
    }
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    if (!keyWindow) {
        keyWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    if (keyWindow.layer.sublayers.count == self.windowSubviews) {
        NSLog(@"airplay removed");
        [keyWindow removeObserver:self forKeyPath:@"layer.sublayers"];
    }
}

请注意,UIKit 中的任何内容都不能保证与 KVO 兼容,因此我们可以观察其层的子层,而不是观察窗口的子视图。

正如 lxt 所说,这很容易破坏,但就我的目的而言,它会完成这项工作。

于 2013-10-06T15:34:45.687 回答