Reputation: 6276
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?
How can I programatically determine if the AirPlay popover view is visible?
Upvotes: 2
Views: 632
Reputation: 6276
With lxt's guidance I managed to put something together. It isn't ideal but it seems to work for me.
I added a tap gesture to the airplay button to know when to start observing the key window. When it's tapped, I start observing.
- (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"];
}
}
Note that nothing in UIKit is guaranteed to be KVO compliant, so instead of observing the window's subviews we can observe its layer's sublayers.
As lxt said, this could break very easily, but for my purposes it will do the job.
Upvotes: 1
Reputation: 31304
First of all, there's no method in the public API that would allow you to do this.
If you did want to try, one way I can think of would be to observe the main window of your application and look out for whenever a popover view gets added to the view hierarchy. By further observing this popover you should be able to tell when it is dismissed.
However, this is really hacky because it relies on the underlying implementation not changing (which it can, and inevitably will, in future iOS releases).
Upvotes: 3