加速度、重力感应和陀螺仪、、

#import <CoreMotion/CoreMotion.h>  //!!!!!!

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *leftRightLabel;
@property (weak, nonatomic) IBOutlet UILabel *upDownLabel;
@property (weak, nonatomic) IBOutlet UILabel *rollLabel;

@property (weak, nonatomic) IBOutlet UILabel *addX;
@property (weak, nonatomic) IBOutlet UILabel *addY;
@property (weak, nonatomic) IBOutlet UILabel *addZ;

@property (weak, nonatomic) IBOutlet UILabel *gravityX;
@property (weak, nonatomic) IBOutlet UILabel *gravityY;
@property (weak, nonatomic) IBOutlet UILabel *gravityZ;

@property (weak, nonatomic) IBOutlet UILabel *rotationRateX;
@property (weak, nonatomic) IBOutlet UILabel *rotationRateY;
@property (weak, nonatomic) IBOutlet UILabel *rotationRateZ;

/**
 *  运动管理器
 */
@property (nonatomic, strong) CMMotionManager *motionManager;


 



#pragma mark 开关按钮点击事件  拖一个switch控件
-(IBAction)motionSwitchHandler:(UISwitch *)sender {
    if (sender.on) {
        //如果是开启的就调用方法 开启并测试
        [self controlHardware];
    } else {
        //否则就停掉感应器
        [self.motionManager stopDeviceMotionUpdates];
    }
}

#pragma mark 控制设备管理器的开启
- (void)controlHardware {
    //开启
    //当前队列, 当前线程
    //实时返回数据
    if ([self.motionManager isDeviceMotionAvailable]) {  //判断该设备是否有传感器
        if ([self.motionManager isDeviceMotionActive]) { //判断传感器是否已开启
            
        } else {
            [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
                //左右摇摆, 仰俯, 滚动
                self.leftRightLabel.text = [NSString stringWithFormat:@"左右摇摆: %.2f", motion.attitude.yaw];
                self.upDownLabel.text = [NSString stringWithFormat:@"仰俯: %.2f", motion.attitude.pitch];
                self.rollLabel.text = [NSString stringWithFormat:@"滚动: %.2f", motion.attitude.roll];
                
                //加速度
                self.addX.text = [NSString stringWithFormat:@"加速度x: %.2f", motion.userAcceleration.x];
                self.addY.text = [NSString stringWithFormat:@"加速度y: %.2f", motion.userAcceleration.y];
                self.addZ.text = [NSString stringWithFormat:@"加速度z: %.2f", motion.userAcceleration.z];
                
                //重力感应
                self.gravityX.text = [NSString stringWithFormat:@"重力感应x: %.2f", motion.gravity.x];
                self.gravityY.text = [NSString stringWithFormat:@"重力感应y: %.2f", motion.gravity.y];
                self.gravityZ.text = [NSString stringWithFormat:@"重力感应z: %.2f", motion.gravity.z];
                
                //陀螺仪
                self.rotationRateX.text = [NSString stringWithFormat:@"自转速率x: %.2f", motion.rotationRate.x];
                self.rotationRateY.text = [NSString stringWithFormat:@"自转速率y: %.2f", motion.rotationRate.y];
                self.rotationRateZ.text = [NSString stringWithFormat:@"自转速率z: %.2f", motion.rotationRate.z];
                
            }];

        }
    }
    
    //停掉
//    [self.motionManager stopDeviceMotionUpdates];
}
<pre name="code" class="objc">

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值