iOS中关闭屏幕旋转功能时如何判断屏幕方向

首先讲强制横屏和竖屏,其实很少App需要强制转屏的,一般在视频播放,相机这类需要旋转屏幕

(1)这段代码是直接横屏 (interfaceOrientation 这个枚举有各种情况

// 视图显示为横屏状态
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
-(BOOL)shouldAutorotate {
    return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft;
}

(2)一般情况可以使用通知来做,但是在屏幕禁止旋转的时候通知也是没效果的,可以参考这个很详细:IOS UIDevice & IOS检测屏幕旋转实例 这里有详细的说明获取真机的信息

(3)所以在屏幕禁止旋转的时候我们只能强制旋转的,

这是手动强制旋转的

// 强制旋转
- (IBAction)tranfromOnClick:(UIButton *)sender
{
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait)
    {
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];//这句话是防止手动先把设备置为横屏,导致下面的语句失效.
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
    } else {
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];//这句话是防止手动先把设备置为竖屏,导致下面的语句失效.
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
    }
}
之后是要再强制的情况下手动旋转,当我们横着的时候屏幕也要旋转到横屏,竖屏的时候屏幕也要旋转到竖屏,这里我们就只能用  螺旋仪和加速器 来判断了

#import <CoreMotion/CoreMotion.h> 首先加入这么框架

创建两个对象
@property (strong, nonatomic) CMMotionManager *motionManager;

@property (assign, nonatomic) BOOL isHeng;   // 判断横竖屏

/**
 *  在真机关闭屏幕旋转功能时如何去判断屏幕方向
 */
- (void)initMotionManager
{
    if (_motionManager == nil) {
        _motionManager = [[CMMotionManager alloc] init];
    }
    
    // 提供设备运动数据到指定的时间间隔
    _motionManager.deviceMotionUpdateInterval = .3;
    
    if (_motionManager.deviceMotionAvailable) {  // 确定是否使用任何可用的态度参考帧来决定设备的运动是否可用
        // 启动设备的运动更新,通过给定的队列向给定的处理程序提供数据。
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
            
            [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
        }];
    }else
    {
        [self setMotionManager:nil];
    }
}

- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion
{
    double x = deviceMotion.gravity.x;
    double y = deviceMotion.gravity.y;
    
    if (fabs(y) >= fabs(x))
    {  // NSLog(@"竖屏");
        if (self.isHeng == YES) {
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];//这句话是防止手动先把设备置为竖屏,导致下面的语句失效.
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
            self.isHeng = NO;
        }
    }
    else
    {       // NSLog(@"横屏");
        if (self.isHeng == NO) {
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];//这句话是防止手动先把设备置为横屏,导致下面的语句失效.
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
            self.isHeng = YES;
        }
    }
}

源码: 下载源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值