iOS判断设备屏幕方向

1
2
@property (nonatomic, strong) CMMotionManager* manager;
@property (nonatomic, strong) NSOperationQueue *queque;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- (void)startMinitorOrientation
{
self.manager.deviceMotionUpdateInterval = 1;
if (self.manager.deviceMotionAvailable == YES)
{
__weak typeof(self) weak_self = self;
[self.manager startDeviceMotionUpdatesToQueue:self.queque withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
[weak_self handleDeviceMotion:motion];
}];
}
else
{
self.manager = nil;
}
}

- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion
{
double x = deviceMotion.gravity.x;
double y = deviceMotion.gravity.y;
if (fabs(y) >= fabs(x))
{
if (y >= 0)
{
NSLog(@"反竖屏");
}
else
{
NSLog(@"竖屏");
}
}
else
{
if (x >= 0)
{
NSLog(@"右横屏");
}
else
{
NSLog(@"左横屏");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-(NSOperationQueue*)queque
{
if (!_queque)
{
_queque=[[NSOperationQueue alloc] init];
}
return _queque;
}

-(CMMotionManager*)manager
{
if (!_manager)
{
_manager=[[CMMotionManager alloc] init];
}
return _manager;
}