當h5裏面有video 標籤的時候,監聽video播放進入全屏,和退出全屏通知
1、添加監聽UIWindowDidResignKeyNotification
、UIWindowDidBecomeHiddenNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoBeginFullScreen)
name:UIWindowDidResignKeyNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoStopFullScreen)
name:UIWindowDidBecomeHiddenNotification
object:nil];
2、 實現監聽事件
- (void)videoBeginFullScreen {
//自定義實現
}
- (void)videoStopFullScreen {
//自定義實現
}
3、移除監聽事件,這個根據需要在合適的地方移除
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];
4、注意的地方
添加通知的時候,如果是監聽video進入全屏,同時也存在輸入框的情況下,就不能使用下面的這個通知的監聽 UIWindowDidBecomeVisibleNotification
,因爲無論是鍵盤彈起還是video進入全屏,都會觸發該方法,如果不注意的話,可能會導致某些邏輯錯誤。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoBeginFullScreen)
name:UIWindowDidBecomeVisibleNotification
object:nil];
5 、關於
UIKIT_EXTERN NSString *const UIWindowDidBecomeVisibleNotification; // 當window激活時並展示在界面的時候觸發,返回空
UIKIT_EXTERN NSString *const UIWindowDidBecomeHiddenNotification; // 當window隱藏的時候觸發,暫時沒有實際測,返回空
UIKIT_EXTERN NSString *const UIWindowDidBecomeKeyNotification; // 當window被設置爲keyWindow時觸發,返回空
UIKIT_EXTERN NSString *const UIWindowDidResignKeyNotification; // 當window的key位置被取代時觸發,返回空
6、參考文檔
https://developer.apple.com/documentation/uikit/uiwindowdidresignkeynotification?language=objc