• 加速度获取


    加速度通过CMMotionManager来获取

    (一)头文件

    #import "AccViewController.h"
    #import <CoreMotion/CoreMotion.h>
    
    @interface AccViewController ()<UIAccelerometerDelegate>
    {
        NSTimer *updateTimer;
    }
    
    @property(nonatomic,strong) CMMotionManager *motionManager;
    
    @end

    (二)判断是否可用

       _motionManager=[[CMMotionManager alloc] init];
        if(_motionManager.deviceMotionAvailable)
        {
            [_motionManager startDeviceMotionUpdates];
        }else
        {
            NSLog(@"Motion unavailable");
        }

    (三)获取当前运动状态

    -(void) updateAcc
    {
        NSLog(@"Update acc");
        if(_motionManager.deviceMotionAvailable)
        {
            CMDeviceMotion *motion=_motionManager.deviceMotion;
            accXvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.x];
            accYvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.y];
            accZvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.z];
        }
    }
    可以通过CMDeviceMotion 获取当前手机的各种运动状态,不止上述中的三种。

    (四)通过定时器实时更新
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        //使用定时器周期性获取设备移动信息
        updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateAcc) userInfo:nil repeats:YES];    [updateTimer fire];
        [updateTimer fire];
    }
    
    
    
    

    ?//----------------------------------------------------完整代码/----------------------------------------------------//

    #import "AccViewController.h"
    #import <CoreMotion/CoreMotion.h>
    
    @interface AccViewController ()<UIAccelerometerDelegate>
    {
        NSTimer *updateTimer;
    }
    
    @property(nonatomic,strong) CMMotionManager *motionManager;
    
    @end
    
    
    UILabel *accXvalue;
    UILabel *accYvalue;
    UILabel *accZvalue;
    
    @implementation AccViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor=[UIColor greenColor];
        
        UILabel *accX=[[UILabel alloc] init];
        accX.text=@"acc_X:";
        accX.frame=CGRectMake(100, 200, 100, 15);
        [self.view addSubview:accX];
        
        accXvalue=[[UILabel alloc] init];
        accXvalue.text=@"";
        accXvalue.frame=CGRectMake(210, 200, 150, 15);
        [self.view addSubview:accXvalue];
        
        UILabel *accY=[[UILabel alloc] init];
        accY.text=@"acc_Y:";
        accY.frame=CGRectMake(100, 300, 100, 15);
        [self.view addSubview:accY];
        
        accYvalue=[[UILabel alloc] init];
        accYvalue.text=@"";
        accYvalue.frame=CGRectMake(210, 300, 150, 15);
        [self.view addSubview:accYvalue];
        
        UILabel *accZ=[[UILabel alloc] init];
        accZ.text=@"acc_Z:";
        accZ.frame=CGRectMake(100, 400, 100, 15);
        [self.view addSubview:accZ];
        
        accZvalue=[[UILabel alloc] init];
        accZvalue.text=@"";
        accZvalue.frame=CGRectMake(210, 400, 150, 15);
        [self.view addSubview:accZvalue];
        
        _motionManager=[[CMMotionManager alloc] init];
        if(_motionManager.deviceMotionAvailable)
        {
            [_motionManager startDeviceMotionUpdates];
        }else
        {
            NSLog(@"Motion unavailable");
        }
        
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        //使用定时器周期性获取设备移动信息
        updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateAcc) userInfo:nil repeats:YES];    [updateTimer fire];
        [updateTimer fire];
    }
    
    -(void) updateAcc
    {
        NSLog(@"Update acc");
        if(_motionManager.deviceMotionAvailable)
        {
            CMDeviceMotion *motion=_motionManager.deviceMotion;
            accXvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.x];
            accYvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.y];
            accZvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.z];
        }
    }
     
  • 相关阅读:
    malloc/free和new/delete的区别
    Vim Skills——Windows利用Vundle和Github进行Vim配置和插件的同步
    selenium自学笔记---ecshop购买脚本 xpath定位元素(下拉框,单选框)
    python基础09--闭包,装饰器
    python基础08--迭代器,生成器
    python基础07--函数
    python基础06--文件操作
    python基础05--深浅copy, set,bytes
    python基础04--list,cou,dict
    python基础03--int,bool,str
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9722055.html
Copyright © 2020-2023  润新知