朋友公司是做教育的,APP中讲课画板是需要在横屏过程中全屏显示,自己已经实现横竖屏切换,但是老板有这样一个需求,假如用户开启了横屏锁,需要提醒用户将横屏锁打开。于是找到了我,问我有没有 系统提供的一个API 供我们调用,如果有,就直接调用解决了。
于是我换了种思路来解决这个问题。思路是这样的,利用重力感应器来判断用户当前的屏幕方向,利用系统横竖屏通知获取当前横竖屏状态;如果在重力感应获取到用户是横屏状态下,但是系统没有通知过来,那就提醒用户开启横屏锁,否则就正常操作。 代码实现:
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
{
BOOL canRotate;//标志位:判断现在能不能旋转屏幕
}
@property (nonatomic, strong) CMMotionManager * motionManager;
@end
- (void)viewDidLoad {
[super viewDidLoad];
canRotate = NO;//这个参数是整个问题解决的核心,注意它的变化
[self startMotionManager];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDeviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil
];
}
//这个是系统横竖屏通知过来,自己需要操作的方法
- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
//1.获取 当前设备 实例
UIDevice *device = [UIDevice currentDevice] ;
/**
* 2.取得当前Device的方向,Device的方向类型为Integer
*
* 必须调用beginGeneratingDeviceOrientationNotifications方法后,此orientation属性才有效,否则一直是0。orientation用于判断设备的朝向,与应用UI方向无关
*
* @param device.orientation
*
*/
switch (device.orientation) {
//其他情况屏幕方向就不一一列举出来了
case UIDeviceOrientationLandscapeLeft:
NSLog(@"屏幕向左横置");
canRotate = YES;//只有当用户把手机旋转到横屏的时候来去触发判断是否支持横屏,如果不支持就提醒用户
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"屏幕向右橫置");
canRotate = YES;
break;
default:
break;
}
}
//初始化
- (void)startMotionManager{
if (_motionManager == nil) {
_motionManager = [[CMMotionManager alloc] init];
}
_motionManager.deviceMotionUpdateInterval = 1/15.0;//多长时间刷新一次
if (_motionManager.deviceMotionAvailable) {
NSLog(@"Device Motion Available");
[_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMDeviceMotion *motion, NSError *error){
[self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
}];
} else {
NSLog(@"No device motion on device.");
[self setMotionManager:nil];
}
}
//这里是重力感应的处理方法
- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion{
double x = deviceMotion.gravity.x;
double y = deviceMotion.gravity.y;
if (fabs(y) >= fabs(x))
{
if (y >= 0){
// UIDeviceOrientationPortraitUpsideDown;
}
else{
// UIDeviceOrientationPortrait;
}
}
else
{
if(canRotate == NO)
{
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提醒" message:@"您关闭了横屏锁,请在控制中心打开" preferredStyle:UIAlertControllerStyleAlert];
[ac addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:ac animated:YES completion:nil];
}
if (x >= 0){
// UIDeviceOrientationLandscapeRight;
}
else{
// UIDeviceOrientationLandscapeLeft;
}
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[_motionManager stopDeviceMotionUpdates];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
结束 如果有异议或者更好的方式欢迎留言! (责任编辑:好模板) |


ecshop内衣模板兰缪2010版
人气:723
Glory外贸商城多维移动化
人气:235
空包代发刷快递单空包网
人气:3590
红孩子ecshop模板
人气:578
ecshop电器商城模板
人气:497
ecshop眼镜商城|ecshop红色模
人气:1241