我正在学习使用 iOS 中的陀螺仪传感器编写应用。是否有处理陀螺仪的类,类似于加速度计的 UIAcceleration/UIAccelerometer/UIAccelerometerDelegate?
最佳答案
首先导入CoreMotion
框架
#import <CoreMotion/CoreMotion.h>
self.motionManager = [[CMMotionManager alloc] init];
//Gyroscope
if([self.motionManager isGyroAvailable])
{
/* Start the gyroscope if it is not active already */
if([self.motionManager isGyroActive] == NO)
{
/* Update us 2 times a second */
[self.motionManager setGyroUpdateInterval:1.0f / 2.0f];
/* Add on a handler block object */
/* Receive the gyroscope data on this block */
[self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMGyroData *gyroData, NSError *error)
{
NSString *x = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.x];
self.gyro_xaxis.text = x;
NSString *y = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.y];
self.gyro_yaxis.text = y;
NSString *z = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.z];
self.gyro_zaxis.text = z;
}];
}
}
else
{
NSLog(@"Gyroscope not Available!");
}
正如代码所说,首先我创建了一个运动管理器的实例。然后我看看设备是否支持陀螺仪。如果没有优雅地死去,否则设置陀螺仪更新间隔然后注册以从陀螺仪获取更新。通过这些更新,您需要定义要对这些值执行的操作的自定义逻辑。就这样,你可以开始了......
关于iphone - iOS 陀螺仪 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7135934/