美文网首页iOS个人修养IOS开发IOS
CoreMotionManager的简单使用

CoreMotionManager的简单使用

作者: CGPointZero | 来源:发表于2015-12-01 11:33 被阅读382次

    根据CoreMotionManager可以获取一些角度,比如拍照时,相机的十字准星与地平线的夹角、手机水平放置时,绕长边抬起的角度(俯仰角)等。
    <pre>#import "ViewController.h"

    import <CoreMotion/CoreMotion.h>

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad
      {
      [super viewDidLoad];
      self.view.backgroundColor=[UIColor whiteColor];
      [ViewController getDegress];

    }
    //各种角度信息
    +(void)getDegress
    {
    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    if (!motionManager.accelerometerAvailable) {
    NSLog(@"没有加速计");
    }
    //更新频率是10Hz
    motionManager.accelerometerUpdateInterval = 0.1;

    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
        //地平线夹角
        NSLog(@"aaaaa%.0f",(M_PI_2-atan2(motionManager.deviceMotion.gravity.x, motionManager.deviceMotion.gravity.z))\*180/M_PI);
        //俯仰角
        NSLog(@"bbbbb%.0f",(atan2(motion.gravity.z, motion.gravity.x)+M_PI_2)\*180/M_PI);
    }];
    

    }
    @end</pre>

    相关文章

      网友评论

        本文标题:CoreMotionManager的简单使用

        本文链接:https://www.haomeiwen.com/subject/lwpjhttx.html