首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS陀螺仪接口

iOS陀螺仪接口
EN

Stack Overflow用户
提问于 2011-08-21 10:21:26
回答 2查看 24.6K关注 0票数 13

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-21 17:04:30

首先导入CoreMotion框架

代码语言:javascript
复制
#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!");
    }

正如代码所说,首先我创建了一个动作管理器的实例。然后我看看这个设备是否支持陀螺仪。如果不合适,则设置陀螺仪更新间隔,然后注册以从陀螺仪获取更新。使用这些更新,您需要定义您希望如何处理这些值的自定义逻辑。就这样,你可以走了.

票数 31
EN

Stack Overflow用户

发布于 2011-08-21 10:41:17

对于陀螺仪数据,您需要使用CoreMotion。从阅读relevant section of the Event Handling Guide for iOS开始。您需要使用两个类:封装陀螺仪事件数据的CMGyroData和用于注册陀螺仪事件的CMMotionManager

更多信息可以在这个问题的选择答案中找到:Apple gyroscope sample code

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7135934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档