ios8 – 如何检测AVPlayerViewController的全屏模式

前端之家收集整理的这篇文章主要介绍了ios8 – 如何检测AVPlayerViewController的全屏模式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何检测用户按下AVPlayerViewController的展开图标?
我想知道电影播放时是否进入全屏模式.

解决方法

也可以观察playerViewController.contentOverlayView的界限,并将其与[UIScreen mainScreen] .bounds进行比较,例如:
self.playerViewController = [AVPlayerViewController new];
// do this after adding player VC as a child VC or in completion block of -presentViewController:animated:completion:
[self.playerViewController.contentOverlayView addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];

...

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if (object == self.playerViewController.contentOverlayView) {
        if ([keyPath isEqualToString:@"bounds"]) {
            CGRect oldBounds = [change[NSKeyValueChangeOldKey] CGRectValue],newBounds = [change[NSKeyValueChangeNewKey] CGRectValue];
            BOOL wasFullscreen = CGRectEqualToRect(oldBounds,[UIScreen mainScreen].bounds),isFullscreen = CGRectEqualToRect(newBounds,[UIScreen mainScreen].bounds);
            if (isFullscreen && !wasFullscreen) {
                if (CGRectEqualToRect(oldBounds,CGRectMake(0,newBounds.size.height,newBounds.size.width))) {
                    NSLog(@"rotated fullscreen");
                }
                else {
                    NSLog(@"entered fullscreen");
                }
            }
            else if (!isFullscreen && wasFullscreen) {
                NSLog(@"exited fullscreen");
            }
        }
    }
}

猜你在找的iOS相关文章