OC 重力感应转屏

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

-(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];
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 今天看到一篇不错的文章关于OC内存管理的,转载一下与你共享概述我们知道在程序运行过程中要创建大量的对象,和其他高级...
    niceSYT阅读 3,308评论 0 2
  • 《原文地址》 GPUImage是现在做滤镜最主流的开源框架,没有之一。作者BradLarson基于openGL对图...
    泛坷阅读 4,082评论 0 1
  • GPUImage接入 下载GPUImage 地址 : https://github.com/BradLarson/...
    夜空丶阅读 9,132评论 1 1
  • 1、在这节课最重要印象最深刻的三个部分/知识点? (1)小组讲课 (2)发送邮件 (3)联觉的知识点 2、为什么这...
    MY马悦阅读 1,427评论 0 0
  • 戴上太阳镜片,如果我嘴角不露出表情,没人能看出来我眼睛的状态,哈哈哈哈哈 但这并不会隐身,至少身体还在 不懂的人时...
    我不是注销了吗阅读 1,391评论 2 0

友情链接更多精彩内容