CoreMotionManager的简单使用

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

#import "ViewController.h"

import

@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

CoreMotionManager的简单使用_第1张图片

你可能感兴趣的:(CoreMotionManager的简单使用)