iOS 陀螺仪 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7135934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:12:06  来源:igfitidea点击:

iOS Gyroscope API

iphoneobjective-ciosipadgyroscope

提问by user682765

I am learning to write an app using the gyroscope sensor in iOS. Are there classes for dealing with the gyroscope similar to UIAcceleration/UIAccelerometer/UIAccelerometerDelegate for the accelerometer?

我正在学习使用 iOS 中的陀螺仪传感器编写应用程序。是否有类用于处理类似于加速度计的 UIAcceleration/UIAccelerometer/UIAccelerometerDelegate 的陀螺仪?

回答by Srikar Appalaraju

First import CoreMotionframework

第一个导入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!");
    }

As the code says, first I create an instance of motion manager. Then I see if the device supports Gyroscope. If not die gracefully, else set gyroscope update interval & then register to get updates from gyroscope. With these updates you need to define your custom logic of what you want to do with the values. That's it you are good to go...

正如代码所说,首先我创建了一个运动管理器的实例。然后我看看设备是否支持陀螺仪。如果没有优雅地死亡,则设置陀螺仪更新间隔,然后注册以从陀螺仪获取更新。通过这些更新,您需要定义要对值执行的操作的自定义逻辑。这样你就可以走了......

回答by Carter Allen

For gyroscope data, you'll need to use CoreMotion. Get started by reading the relevant section of the Event Handling Guide for iOS. You'll need to work with two classes: CMGyroDatawhich encapsulates gyroscope event data, and CMMotionManagerwhich is used to register for gyroscope events.

对于陀螺仪数据,您需要使用 CoreMotion。首先阅读iOS 事件处理指南相关部分。你需要工作两个类:CMGyroData它封装陀螺仪事件数据,并CMMotionManager其用于为陀螺仪注册事件。

More information can be found in this question's selected answer: Apple gyroscope sample code

更多信息可以在这个问题的选定答案中找到:Apple陀螺仪示例代码