123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- // ZFOrentationObserver.m
- // ZFPlayer
- //
- // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #import "ZFOrientationObserver.h"
- #import "ZFPlayer.h"
- #define SysVersion [[UIDevice currentDevice] systemVersion].floatValue
- @interface ZFFullViewController : UIViewController
- @property (nonatomic, assign) UIInterfaceOrientationMask interfaceOrientationMask;
- @end
- @implementation ZFFullViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- }
- - (BOOL)shouldAutorotate {
- return YES;
- }
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
- if (self.interfaceOrientationMask) {
- return self.interfaceOrientationMask;
- }
- return UIInterfaceOrientationMaskLandscape;
- }
- @end
- @interface UIWindow (CurrentViewController)
- /*!
- @method currentViewController
- @return Returns the topViewController in stack of topMostController.
- */
- + (UIViewController*)zf_currentViewController;
- @end
- @implementation UIWindow (CurrentViewController)
- + (UIViewController*)zf_currentViewController; {
- UIWindow *window = [[UIApplication sharedApplication].delegate window];
- UIViewController *topViewController = [window rootViewController];
- while (true) {
- if (topViewController.presentedViewController) {
- topViewController = topViewController.presentedViewController;
- } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
- topViewController = [(UINavigationController *)topViewController topViewController];
- } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
- UITabBarController *tab = (UITabBarController *)topViewController;
- topViewController = tab.selectedViewController;
- } else {
- break;
- }
- }
- return topViewController;
- }
- @end
- @interface ZFOrientationObserver ()
- @property (nonatomic, weak) UIView *view;
- @property (nonatomic, assign, getter=isFullScreen) BOOL fullScreen;
- @property (nonatomic, strong) UIView *cell;
- @property (nonatomic, assign) NSInteger playerViewTag;
- @property (nonatomic, assign) ZFRotateType roateType;
- @property (nonatomic, strong) UIView *blackView;
- @property (nonatomic, strong) UIWindow *customWindow;
- @end
- @implementation ZFOrientationObserver
- - (instancetype)init {
- self = [super init];
- if (self) {
- _duration = 0.30;
- _fullScreenMode = ZFFullScreenModeLandscape;
- _supportInterfaceOrientation = ZFInterfaceOrientationMaskAllButUpsideDown;
- _allowOrentitaionRotation = YES;
- _roateType = ZFRotateTypeNormal;
- _currentOrientation = UIInterfaceOrientationPortrait;
- }
- return self;
- }
- - (void)updateRotateView:(UIView *)rotateView
- containerView:(UIView *)containerView {
- self.view = rotateView;
- self.containerView = containerView;
- }
- - (void)cellModelRotateView:(UIView *)rotateView rotateViewAtCell:(UIView *)cell playerViewTag:(NSInteger)playerViewTag {
- self.roateType = ZFRotateTypeCell;
- self.view = rotateView;
- self.cell = cell;
- self.playerViewTag = playerViewTag;
- }
- - (void)cellOtherModelRotateView:(UIView *)rotateView containerView:(UIView *)containerView {
- self.roateType = ZFRotateTypeCellOther;
- self.view = rotateView;
- self.containerView = containerView;
- }
- - (void)dealloc {
- [self removeDeviceOrientationObserver];
- [self.blackView removeFromSuperview];
- }
- - (void)addDeviceOrientationObserver {
- if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
- [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
- }
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
- }
- - (void)removeDeviceOrientationObserver {
- if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
- [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
- }
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
- }
- - (void)handleDeviceOrientationChange {
- if (self.fullScreenMode == ZFFullScreenModePortrait || !self.allowOrentitaionRotation) return;
- UIInterfaceOrientation currentOrientation = UIInterfaceOrientationUnknown;
- if (UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation)) {
- currentOrientation = (UIInterfaceOrientation)[UIDevice currentDevice].orientation;
- } else {
- return;
- }
- // Determine that if the current direction is the same as the direction you want to rotate, do nothing
- if (currentOrientation == _currentOrientation && !self.forceDeviceOrientation) return;
-
- switch (currentOrientation) {
- case UIInterfaceOrientationPortrait: {
- if ([self isSupportedPortrait]) {
- [self enterLandscapeFullScreen:UIInterfaceOrientationPortrait animated:YES];
- }
- }
- break;
- case UIInterfaceOrientationLandscapeLeft: {
- if ([self isSupportedLandscapeLeft]) {
- [self enterLandscapeFullScreen:UIInterfaceOrientationLandscapeLeft animated:YES];
- }
- }
- break;
- case UIInterfaceOrientationLandscapeRight: {
- if ([self isSupportedLandscapeRight]) {
- [self enterLandscapeFullScreen:UIInterfaceOrientationLandscapeRight animated:YES];
- }
- }
- break;
- default: break;
- }
- }
- - (void)forceDeviceOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated {
- UIView *superview = nil;
- if (UIInterfaceOrientationIsLandscape(orientation)) {
- /// It's not set from the other side of the screen to this side
- if (!self.isFullScreen) {
- self.view.frame = [self.view convertRect:self.view.frame toView:superview];
- }
- self.fullScreen = YES;
- superview = self.fullScreenContainerView;
- } else {
- if (!self.fullScreen) return;
- self.fullScreen = NO;
- if (self.roateType == ZFRotateTypeCell) superview = [self.cell viewWithTag:self.playerViewTag];
- else superview = self.containerView;
- if (self.blackView.superview != nil) [self.blackView removeFromSuperview];
- }
- if (self.orientationWillChange) self.orientationWillChange(self, self.isFullScreen);
- [UIViewController attemptRotationToDeviceOrientation];
- [superview addSubview:self.view];
- if (animated) {
- [UIView animateWithDuration:self.duration animations:^{
- self.view.frame = superview.bounds;
- [self.view layoutIfNeeded];
- [self interfaceOrientation:orientation];
- } completion:^(BOOL finished) {
- if (self.fullScreen) {
- [superview insertSubview:self.blackView belowSubview:self.view];
- self.blackView.frame = superview.bounds;
- }
- if (self.orientationDidChanged) self.orientationDidChanged(self, self.isFullScreen);
- }];
- } else {
- self.view.frame = superview.bounds;
- [self.view layoutIfNeeded];
- [UIView animateWithDuration:0 animations:^{
- [self interfaceOrientation:orientation];
- }];
- if (self.fullScreen) {
- [superview insertSubview:self.blackView belowSubview:self.view];
- self.blackView.frame = superview.bounds;
- }
- if (self.orientationDidChanged) self.orientationDidChanged(self, self.isFullScreen);
- }
- }
- - (void)normalOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated {
- UIView *superview = nil;
- CGRect frame;
- if (UIInterfaceOrientationIsLandscape(orientation)) {
- superview = self.fullScreenContainerView;
- /// It's not set from the other side of the screen to this side
- if (!self.isFullScreen) {
- self.view.frame = [self.view convertRect:self.view.frame toView:superview];
- }
- [superview addSubview:self.view];
- self.fullScreen = YES;
- if (self.orientationWillChange) self.orientationWillChange(self, self.isFullScreen);
-
- ZFFullViewController *fullVC = [[ZFFullViewController alloc] init];
- if (orientation == UIInterfaceOrientationLandscapeLeft) {
- fullVC.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeLeft;
- } else {
- fullVC.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;
- }
- self.customWindow.rootViewController = fullVC;
- } else {
- self.fullScreen = NO;
- if (self.orientationWillChange) self.orientationWillChange(self, self.isFullScreen);
- ZFFullViewController *fullVC = [[ZFFullViewController alloc] init];
- fullVC.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;
- self.customWindow.rootViewController = fullVC;
-
- if (self.roateType == ZFRotateTypeCell) superview = [self.cell viewWithTag:self.playerViewTag];
- else superview = self.containerView;
- if (self.blackView.superview != nil) [self.blackView removeFromSuperview];
- }
- frame = [superview convertRect:superview.bounds toView:self.fullScreenContainerView];
-
- if (animated) {
- [UIView animateWithDuration:self.duration animations:^{
- self.view.transform = [self getTransformRotationAngle:orientation];
- [UIView animateWithDuration:self.duration animations:^{
- self.view.frame = frame;
- [self.view layoutIfNeeded];
- }];
- } completion:^(BOOL finished) {
- [superview addSubview:self.view];
- self.view.frame = superview.bounds;
- if (self.fullScreen) {
- [superview insertSubview:self.blackView belowSubview:self.view];
- self.blackView.frame = superview.bounds;
- }
- if (self.orientationDidChanged) self.orientationDidChanged(self, self.isFullScreen);
- }];
- } else {
- self.view.transform = [self getTransformRotationAngle:orientation];
- [superview addSubview:self.view];
- self.view.frame = superview.bounds;
- [self.view layoutIfNeeded];
- if (self.fullScreen) {
- [superview insertSubview:self.blackView belowSubview:self.view];
- self.blackView.frame = superview.bounds;
- }
- if (self.orientationDidChanged) self.orientationDidChanged(self, self.isFullScreen);
- }
- }
- - (void)interfaceOrientation:(UIInterfaceOrientation)orientation {
- if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
- SEL selector = NSSelectorFromString(@"setOrientation:");
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
- [invocation setSelector:selector];
- [invocation setTarget:[UIDevice currentDevice]];
- UIInterfaceOrientation val = orientation;
- [invocation setArgument:&val atIndex:2];
- [invocation invoke];
- }
- }
- /// Gets the rotation Angle of the transformation.
- - (CGAffineTransform)getTransformRotationAngle:(UIInterfaceOrientation)orientation {
- if (orientation == UIInterfaceOrientationPortrait) {
- return CGAffineTransformIdentity;
- } else if (orientation == UIInterfaceOrientationLandscapeLeft) {
- return CGAffineTransformMakeRotation(-M_PI_2);
- } else if(orientation == UIInterfaceOrientationLandscapeRight) {
- return CGAffineTransformMakeRotation(M_PI_2);
- }
- return CGAffineTransformIdentity;
- }
- #pragma mark - public
- - (void)enterLandscapeFullScreen:(UIInterfaceOrientation)orientation animated:(BOOL)animated {
- if (self.fullScreenMode == ZFFullScreenModePortrait) return;
- _currentOrientation = orientation;
- if (self.forceDeviceOrientation) {
- [self forceDeviceOrientation:orientation animated:animated];
- } else {
- [self normalOrientation:orientation animated:animated];
- }
- }
- - (void)enterPortraitFullScreen:(BOOL)fullScreen animated:(BOOL)animated {
- if (self.fullScreenMode == ZFFullScreenModeLandscape) return;
- UIView *superview = nil;
- if (fullScreen) {
- superview = self.fullScreenContainerView;
- self.view.frame = [self.view convertRect:self.view.frame toView:superview];
- [superview addSubview:self.view];
- self.fullScreen = YES;
- } else {
- if (self.roateType == ZFRotateTypeCell) {
- superview = [self.cell viewWithTag:self.playerViewTag];
- } else {
- superview = self.containerView;
- }
- self.fullScreen = NO;
- }
- if (self.orientationWillChange) self.orientationWillChange(self, self.isFullScreen);
- CGRect frame = [superview convertRect:superview.bounds toView:self.fullScreenContainerView];
- if (animated) {
- [UIView animateWithDuration:self.duration animations:^{
- self.view.frame = frame;
- [self.view layoutIfNeeded];
- } completion:^(BOOL finished) {
- [superview addSubview:self.view];
- self.view.frame = superview.bounds;
- if (self.orientationDidChanged) self.orientationDidChanged(self, self.isFullScreen);
- }];
- } else {
- [superview addSubview:self.view];
- self.view.frame = superview.bounds;
- [self.view layoutIfNeeded];
- if (self.orientationDidChanged) self.orientationDidChanged(self, self.isFullScreen);
- }
- }
- - (void)exitFullScreenWithAnimated:(BOOL)animated {
- if (self.fullScreenMode == ZFFullScreenModeLandscape) {
- [self enterLandscapeFullScreen:UIInterfaceOrientationPortrait animated:animated];
- } else if (self.fullScreenMode == ZFFullScreenModePortrait) {
- [self enterPortraitFullScreen:NO animated:animated];
- }
- }
- #pragma mark - private
- /// is support portrait
- - (BOOL)isSupportedPortrait {
- return self.supportInterfaceOrientation & ZFInterfaceOrientationMaskPortrait;
- }
- /// is support landscapeLeft
- - (BOOL)isSupportedLandscapeLeft {
- return self.supportInterfaceOrientation & ZFInterfaceOrientationMaskLandscapeLeft;
- }
- /// is support landscapeRight
- - (BOOL)isSupportedLandscapeRight {
- return self.supportInterfaceOrientation & ZFInterfaceOrientationMaskLandscapeRight;
- }
- #pragma mark - getter
- - (UIView *)blackView {
- if (!_blackView) {
- _blackView = [UIView new];
- _blackView.backgroundColor = [UIColor blackColor];
- }
- return _blackView;
- }
- - (UIWindow *)customWindow {
- if (!_customWindow) {
- if (@available(iOS 13.0, *)) {
- UIWindowScene *windowScene = nil;
- for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
- if (scene.activationState == UISceneActivationStateForegroundActive) {
- windowScene = (UIWindowScene *)scene;
- }
- if (!windowScene && [UIApplication sharedApplication].connectedScenes.count == 1) {
- windowScene = (UIWindowScene *)scene;
- }
- }
- if (windowScene) {
- _customWindow = [[UIWindow alloc] initWithWindowScene:windowScene];
- } else {
- _customWindow = [[UIWindow alloc] initWithFrame:CGRectZero];
- }
- } else {
- _customWindow = [[UIWindow alloc] initWithFrame:CGRectZero];
- }
- }
- return _customWindow;
- }
- #pragma mark - setter
- - (void)setLockedScreen:(BOOL)lockedScreen {
- _lockedScreen = lockedScreen;
- if (lockedScreen) {
- [self removeDeviceOrientationObserver];
- } else {
- [self addDeviceOrientationObserver];
- }
- }
- - (UIView *)fullScreenContainerView {
- if (!_fullScreenContainerView) {
- _fullScreenContainerView = [UIApplication sharedApplication].keyWindow;
- }
- return _fullScreenContainerView;
- }
- - (void)setFullScreen:(BOOL)fullScreen {
- _fullScreen = fullScreen;
- [[UIWindow zf_currentViewController] setNeedsStatusBarAppearanceUpdate];
- [UIViewController attemptRotationToDeviceOrientation];
- }
- - (void)setStatusBarHidden:(BOOL)statusBarHidden {
- _statusBarHidden = statusBarHidden;
- [[UIWindow zf_currentViewController] setNeedsStatusBarAppearanceUpdate];
- }
- @end
|