You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
5.7 KiB

/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2020 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// UIInterface+QMUI.m
// QMUIKit
//
// Created by QMUI Team on 2018/12/20.
//
#import "UIInterface+QMUI.h"
#import "QMUICore.h"
@implementation QMUIHelper (QMUI_Interface)
QMUISynthesizeNSIntegerProperty(orientationBeforeChangingByHelper, setOrientationBeforeChangingByHelper)
- (void)handleDeviceOrientationNotification:(NSNotification *)notification {
// 如果是由 setValue:forKey: 方式修改方向而走到这个 notification 的话,理论上是不需要重置为 Unknown 的,但因为在 UIViewController (QMUI) 那边会再次记录旋转前的值,所以这里就算重置也无所谓
[QMUIHelper sharedInstance].orientationBeforeChangingByHelper = UIDeviceOrientationUnknown;
}
+ (BOOL)rotateToDeviceOrientation:(UIDeviceOrientation)orientation {
if ([UIDevice currentDevice].orientation == orientation) {
[UIViewController attemptRotationToDeviceOrientation];
return NO;
}
[[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"];
return YES;
}
+ (UIDeviceOrientation)deviceOrientationWithInterfaceOrientationMask:(UIInterfaceOrientationMask)mask {
if ((mask & UIInterfaceOrientationMaskAll) == UIInterfaceOrientationMaskAll) {
return [UIDevice currentDevice].orientation;
}
if ((mask & UIInterfaceOrientationMaskAllButUpsideDown) == UIInterfaceOrientationMaskAllButUpsideDown) {
return [UIDevice currentDevice].orientation;
}
if ((mask & UIInterfaceOrientationMaskPortrait) == UIInterfaceOrientationMaskPortrait) {
return UIDeviceOrientationPortrait;
}
if ((mask & UIInterfaceOrientationMaskLandscape) == UIInterfaceOrientationMaskLandscape) {
return [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft ? UIDeviceOrientationLandscapeLeft : UIDeviceOrientationLandscapeRight;
}
if ((mask & UIInterfaceOrientationMaskLandscapeLeft) == UIInterfaceOrientationMaskLandscapeLeft) {
return UIDeviceOrientationLandscapeRight;
}
if ((mask & UIInterfaceOrientationMaskLandscapeRight) == UIInterfaceOrientationMaskLandscapeRight) {
return UIDeviceOrientationLandscapeLeft;
}
if ((mask & UIInterfaceOrientationMaskPortraitUpsideDown) == UIInterfaceOrientationMaskPortraitUpsideDown) {
return UIDeviceOrientationPortraitUpsideDown;
}
return [UIDevice currentDevice].orientation;
}
+ (BOOL)interfaceOrientationMask:(UIInterfaceOrientationMask)mask containsDeviceOrientation:(UIDeviceOrientation)deviceOrientation {
if (deviceOrientation == UIDeviceOrientationUnknown) {
return YES;// YES 表示不用额外处理
}
if ((mask & UIInterfaceOrientationMaskAll) == UIInterfaceOrientationMaskAll) {
return YES;
}
if ((mask & UIInterfaceOrientationMaskAllButUpsideDown) == UIInterfaceOrientationMaskAllButUpsideDown) {
return UIInterfaceOrientationPortraitUpsideDown != deviceOrientation;
}
if ((mask & UIInterfaceOrientationMaskPortrait) == UIInterfaceOrientationMaskPortrait) {
return UIInterfaceOrientationPortrait == deviceOrientation;
}
if ((mask & UIInterfaceOrientationMaskLandscape) == UIInterfaceOrientationMaskLandscape) {
return UIInterfaceOrientationLandscapeLeft == deviceOrientation || UIInterfaceOrientationLandscapeRight == deviceOrientation;
}
if ((mask & UIInterfaceOrientationMaskLandscapeLeft) == UIInterfaceOrientationMaskLandscapeLeft) {
return UIInterfaceOrientationLandscapeLeft == deviceOrientation;
}
if ((mask & UIInterfaceOrientationMaskLandscapeRight) == UIInterfaceOrientationMaskLandscapeRight) {
return UIInterfaceOrientationLandscapeRight == deviceOrientation;
}
if ((mask & UIInterfaceOrientationMaskPortraitUpsideDown) == UIInterfaceOrientationMaskPortraitUpsideDown) {
return UIInterfaceOrientationPortraitUpsideDown == deviceOrientation;
}
return YES;
}
+ (BOOL)interfaceOrientationMask:(UIInterfaceOrientationMask)mask containsInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return [self interfaceOrientationMask:mask containsDeviceOrientation:(UIDeviceOrientation)interfaceOrientation];
}
+ (CGFloat)angleForTransformWithInterfaceOrientation:(UIInterfaceOrientation)orientation {
CGFloat angle;
switch (orientation)
{
case UIInterfaceOrientationPortraitUpsideDown:
angle = M_PI;
break;
case UIInterfaceOrientationLandscapeLeft:
angle = -M_PI_2;
break;
case UIInterfaceOrientationLandscapeRight:
angle = M_PI_2;
break;
default:
angle = 0.0;
break;
}
return angle;
}
+ (CGAffineTransform)transformForCurrentInterfaceOrientation {
return [QMUIHelper transformWithInterfaceOrientation:UIApplication.sharedApplication.statusBarOrientation];
}
+ (CGAffineTransform)transformWithInterfaceOrientation:(UIInterfaceOrientation)orientation {
CGFloat angle = [QMUIHelper angleForTransformWithInterfaceOrientation:orientation];
return CGAffineTransformMakeRotation(angle);
}
@end