iOS 重力感应转屏

需要引入#import <CoreMotion/CoreMotion.h>

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
-(CMMotionManager *)motionManager {
if (_motionManager == nil) {
_motionManager = [[CMMotionManager alloc] init];
[_motionManager setDeviceMotionUpdateInterval:1];
}
return _motionManager;
}

//转屏
- (void)startUpdateGyroResult {
if ([self.motionManager isDeviceMotionAvailable] == YES) {
[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
double x = motion.gravity.x;
double y = motion.gravity.y;
double z = motion.gravity.z;
if (fabs(z)<0.5f) {
if (fabs(y) >= fabs(x)) {
if (y <= 0){
//正
if (self->_deviceDirection == XUDeviceNone) {
return ;
}
NSLog(@"正");
self->_deviceDirection = XUDeviceNone;
[UIView animateWithDuration:0.3 animations:^{
self.transform = CGAffineTransformMakeRotation(0);
self.frame = CGRectMake(0, 70, SCREEN_WIDTH, 220);
}];
[self getCurrentViewController].tabBarController.tabBar.hidden = NO;
[[self getCurrentViewController].navigationController setNavigationBarHidden:NO animated:YES];
[[UIApplication sharedApplication]setStatusBarHidden:NO];
}
} else {
if (x >= 0){
//右
if (self->_deviceDirection == XUDeviceRight) {
return ;
}
NSLog(@"右");
self->_deviceDirection = XUDeviceRight;
[UIView animateWithDuration:0.3 animations:^{
// self.transform = CGAffineTransformMakeRotation(M_PI*1.5);
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
} else {
//左
if (self->_deviceDirection == XUDeviceLeft) {
return ;
}
NSLog(@"左");
self->_deviceDirection = XUDeviceLeft;
[UIView animateWithDuration:0.3 animations:^{
// self.transform = CGAffineTransformMakeRotation(M_PI*0.5);
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
}
[self getCurrentViewController].tabBarController.tabBar.hidden = YES;
[[self getCurrentViewController].navigationController setNavigationBarHidden:YES animated:YES];
[[UIApplication sharedApplication]setStatusBarHidden:YES];
}
}
}];
}
}
///停止CMMotionManager
- (void)stopUpdate {
if ([self.motionManager isDeviceMotionAvailable] == YES) {
[self.motionManager stopDeviceMotionUpdates];
}
}
本文总阅读量