14

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?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
user682765
  • 1,229
  • 3
  • 21
  • 33

2 Answers2

31

First import CoreMotion framework

#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...

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • 2
    [self.motionManager isGyroAvailable] is this check a must? What happens if gyroscope is not available? Does the app crash? Or return null values. If it returns null values then which is the object that would return a null?? – user682765 Aug 23 '11 at 16:29
  • 2
    its a check since older iPhones dont have gyroscope. It might crash if you start using gyro API's when the device itself does not support. better to be safe than sorry... – Srikar Appalaraju Aug 24 '11 at 04:49
  • As an update: Apple's Documentation for the `startGyroUpdatesToQueue` recommends not using the main queue as that may cause lag. Either use a different queue or simply use the motion manager's `.gyroData` property to get gyro data. If you're making a game using the motion frameworks, I'd recommend making the interval 1/60 of a second (to match frame rates), and if either of the two have an issue, turn the interval for getting gyro data down. – DDPWNAGE Jul 04 '15 at 05:44
6

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: CMGyroData which encapsulates gyroscope event data, and CMMotionManager which is used to register for gyroscope events.

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

Community
  • 1
  • 1
Carter Allen
  • 1,984
  • 15
  • 22