OC 重力感应转屏

需要引入#import

-(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];
    }
}

你可能感兴趣的:(OC 重力感应转屏)