如何找到Iphone速度和重力值?

时间:2011-05-31 03:16:04

标签: iphone

我正在开发一个应用程序,因为我想要iPhone的速度和重力值。 对于重力,我需要x,y,z轴值如何找到值

请发给我一些关于那个的帖子

感谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

将CoreMotion.framework添加到项目中,然后导入:

#import <CoreMotion/CoreMotion.h>

然后启动一个示例应用并将其添加为按钮操作...

- (IBAction) startUserMotion:(id)sender {

    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    NSMutableArray *motionArray = [[NSMutableArray alloc] init];    

    motionManager.gyroUpdateInterval = 0.50;
    motionManager.accelerometerUpdateInterval = 0.50;
    motionManager.deviceMotionUpdateInterval = 0.50; 
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^ (CMDeviceMotion *devMotion, NSError *error)
     {  
         NSMutableDictionary *motionDetails = [[NSMutableDictionary alloc] init];
        // CMAttitude *currentAttitude = devMotion.attitude;
         CMRotationRate rotation = devMotion.rotationRate;
         CMAcceleration acceleration = devMotion.userAcceleration;
         [motionDetails setValue:[NSNumber numberWithFloat:acceleration.x] forKey:@"AccelerationX"];
         [motionDetails setValue:[NSNumber numberWithFloat:acceleration.y] forKey:@"AccelerationY"];
         [motionDetails setValue:[NSNumber numberWithFloat:acceleration.z] forKey:@"AccelerationZ"];

         [motionDetails setValue:[NSNumber numberWithFloat:rotation.x] forKey:@"RotationX"];
         [motionDetails setValue:[NSNumber numberWithFloat:rotation.y] forKey:@"RotationY"];
         [motionDetails setValue:[NSNumber numberWithFloat:rotation.z] forKey:@"RotationZ"];
         [motionArray addObject:motionDetails];
         [motionDetails release];
     }   ];
}