修改MotionGraphs示例代码



我想通过MotionGraphs保存加速度计或传感器值到一个文件文本。当deviceMotion被激活时,它将在下一个代码中使用:

if ([mManager isDeviceMotionAvailable] == YES) {
    [mManager setDeviceMotionUpdateInterval:updateInterval];
    [mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
        // attitude
        [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeAttitude] addX:deviceMotion.attitude.roll y:deviceMotion.attitude.pitch z:deviceMotion.attitude.yaw];
        //rotationRate
        [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeRotationRate] addX:deviceMotion.rotationRate.x y:deviceMotion.rotationRate.y z:deviceMotion.rotationRate.z];
        // gravity
        [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeGravity] addX:deviceMotion.gravity.x y:deviceMotion.gravity.y z:deviceMotion.gravity.z];
        // userAcceleration
        [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeUserAcceleration] addX:deviceMotion.userAcceleration.x y:deviceMotion.userAcceleration.y z:deviceMotion.userAcceleration.z];

我认为一个队列正在被使用,我想介绍一个实现从这个队列保存到一个文件文本。

这怎么可能呢?

您的代码如下:

NSMutableArray *sensorData;
//[sensorData removeAllObjects];
if ([mManager isDeviceMotionAvailable] == YES) {
    [mManager setDeviceMotionUpdateInterval:updateInterval];
    [mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
        NSString *strSensorData = [NSString stringWithFormat:@"%.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f", deviceMotion.timestamp, deviceMotion.attitude.roll, deviceMotion.attitude.pitch, deviceMotion.attitude.yaw, deviceMotion.rotationRate.x, deviceMotion.rotationRate.y, deviceMotion.rotationRate.z, deviceMotion.gravity.x, deviceMotion.gravity.y, deviceMotion.gravity.z, deviceMotion.userAcceleration.x, deviceMotion.userAcceleration.y, deviceMotion.userAcceleration.z];
        [sensorData addObject:strSensorData];
        //...
}
- (void)stopUpdates
{
    CMMotionManager *mManager = [(APLAppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];
    if ([mManager isDeviceMotionActive] == YES) {
        [mManager stopDeviceMotionUpdates];
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex: 0];
    NSString *docFile = [docDir stringByAppendingPathComponent: @"sensordata.txt"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:docDir])
        [fileManager createFileAtPath:docDir
                             contents:nil
                           attributes:nil];
    //[sensorData writeToFile:docFile atomically:YES];
    [sensorData.description writeToFile:docFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

希望这对你有帮助!

相关内容

  • 没有找到相关文章

最新更新