iphone - iOS 陀螺仪 API

标签 iphone objective-c ios ipad gyroscope

我正在学习使用 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/

相关文章:

ios - Instruments 报告我正在泄漏 NSMallocBlocks(又名 block 对象),但我真的是这样吗?

ios - 想要将一些代码应用到同一个 Storyboard 上的第二个场景(Xcode、Swift 2)

ios - SignificantLocationChanges 不适用于 iOS 8

ios - 如何解决 xcode 中找不到 header 的问题

ios - 没有用于分配给属性的 setter 方法

ios - 如何获取包含在中的自定义单元格的 UIView Controller

iOS 允许在给定时间发生一次触摸事件

iphone - 如何在iPhone应用程序中呈现分层数据?

iphone - 在我的应用程序中使用类似于 iBook 的 3D View 转动过渡是否安全?

objective-c - 如何更改 MFMailComposeViewController 文本和按钮图像的颜色?