我正试图通过加速度计检测到敲击(即在手机背面)。我只能找到这个代码,但大部分都在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;
}
}
谢谢!