#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
/** 运动管理 */
@property (nonatomic, strong) CMMotionManager *mgr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取陀螺仪信息(下面pull方式的时候才可以按需获取数据,这里是用户点击屏幕才获取数据)
CMRotationRate rate = self.mgr.gyroData.rotationRate;
NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
}
#pragma mark - 获取陀螺仪信息
- (void)pullGyro
{
// pull方式
// 1.判断陀螺仪是否可用
if (![self.mgr isGyroAvailable]) {
NSLog(@"手机该换了");
return;
}
// 2.开始采样
[self.mgr startGyroUpdates];
}
- (void)pushGyro
{
// push方式
// 1.判断陀螺仪是否可用
if (![self.mgr isGyroAvailable]) {
NSLog(@"手机该换了");
return;
}
// 2.设置采样间隔
self.mgr.gyroUpdateInterval = 0.3;
// 3.开始采样
[self.mgr startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {
if (error) return;
CMRotationRate rate = gyroData.rotationRate;
NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
}];
}
iPhone陀螺仪
最新推荐文章于 2021-07-29 07:42:35 发布