根据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>
网友评论