iOS 7 - 检测物理敲击/硬拍(不动摇)

时间:2014-08-26 18:27:18

标签: ios core-motion motion

我正试图通过加速度计检测到敲击(即在手机背面)。我只能找到这个代码,但大部分都在iOS 7中被弃用了,我无法弄清楚如何“重新编码”它。

这是我正在使用的代码:

#define kUpdateFrequency    100.0
#define kFilteringFactor    0.1
float accelZ;
int spikeZCount = 0; 

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / kUpdateFrequency];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];

- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration {
accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor)) );

    if (accelZ > 0.0f) {
        if (spikeZCount > 9) {
            //  NSLog(@"SPIKE!");
            [[UIAccelerometer sharedAccelerometer] setDelegate:nil];

            [self isBumped];
        }
        else {
            spikeZCount++;
            //  NSLog(@"spikeZCount %i",spikeZCount);
        }
    }
    else {
        // NSLog(@"spikeZCount Reset");
        spikeZCount = 0;
    }
}

谢谢!

0 个答案:

没有答案