iOS 根据CMMotion重力做旋转通知

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// ViewController.m
// demo
//
// Created by austin on 15/9/25.
// Copyright © 2015年 demo. All rights reserved.
//
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
{
CMMotionManager *motionManager;
}
@property (nonatomic,assign) UIDeviceOrientation lastOrientation;
@end
@implementation ViewController
- (void)viewDidLoad {
motionManager=[[CMMotionManager alloc]init];
__weak typeof(self) weakSelf = self;
if (motionManager.accelerometerAvailable) {
[motionManager setAccelerometerUpdateInterval:0.5f];
NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];
[motionManager startAccelerometerUpdatesToQueue:operationQueue withHandler:^(CMAccelerometerData *data,NSError *error)
{
typeof(self) selfBlock = weakSelf;
CGFloat xx = data.acceleration.x;
CGFloat yy = -data.acceleration.y;
CGFloat zz = data.acceleration.z;
CGFloat device_angle = M_PI / 2.0f - atan2(yy, xx);
UIDeviceOrientation orientation = UIDeviceOrientationUnknown;
if (device_angle > M_PI)
device_angle -= 2 * M_PI;
if ((zz < -.60f) || (zz > .60f)) {
if ( UIDeviceOrientationIsLandscape(selfBlock.lastOrientation) )
orientation = selfBlock.lastOrientation;
else
orientation = UIDeviceOrientationUnknown;
} else {
if ( (device_angle > -M_PI_4) && (device_angle < M_PI_4) )
orientation = UIDeviceOrientationPortrait;
else if ((device_angle < -M_PI_4) && (device_angle > -3 * M_PI_4))
orientation = UIDeviceOrientationLandscapeLeft;
else if ((device_angle > M_PI_4) && (device_angle < 3 * M_PI_4))
orientation = UIDeviceOrientationLandscapeRight;
else
orientation = UIDeviceOrientationPortraitUpsideDown;
}
if (orientation != selfBlock.lastOrientation) {
dispatch_async(dispatch_get_main_queue(), ^{
[selfBlock deviceOrientationDidChangeTo:orientation];
});
}
}];
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)deviceOrientationDidChangeTo:(UIDeviceOrientation )orientation{
switch (orientation) {
case UIDeviceOrientationUnknown:
NSLog(@"UIDeviceOrientationUnknown");
break;
case UIDeviceOrientationPortrait:
NSLog(@"UIDeviceOrientationPortrait");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"UIDeviceOrientationPortraitUpsideDown");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"UIDeviceOrientationLandscapeLeft");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"UIDeviceOrientationLandscapeRight");
break;
case UIDeviceOrientationFaceUp:
NSLog(@"UIDeviceOrientationFaceUp");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"UIDeviceOrientationFaceDown");
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end