Question

I want a label that shows the current g force the iPhone is experiencing. I have code to get information from the accelerometer but I do not know how to send the data to a label to show the current g force. Here is my code...

if (!self.manager) {
    self.manager = [CMMotionManager new];
}

if (self.manager.isAccelerometerActive) {
    [self.manager stopAccelerometerUpdates];
}

NSOperationQueue *queue = [NSOperationQueue new];

[self.manager setAccelerometerUpdateInterval:1.0 / 30.0]; // 30 Updates Per Second
[self.manager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
NSLog(@"X: %f         Y: %f         Z: %f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);
}];

Thanks

Was it helpful?

Solution

It is pretty easy. You'll want to setup a UILabel in Interface Builder and you'll see a property something like this:

    @property (strong, nonatomic) IBOutlet UILabel *myLabel;

Right after your NSLog statement you'll want to insert this code:

    self.myLabel.text = [NSString stringWithFormat:@"x=%f, y=%f, z=%f", accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];

Of course you may want to make it look nicer but this should be enough to get you going.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top