通过加速度计检测iPhone上任何位置的硬敲击 [英] Detect hard taps anywhere on iPhone through accelerometer
问题描述
我正试图检测iPhone上的任何地方的水龙头,而不仅仅是iPhone屏幕。这是一个链接,表明它是可能的。
I am trying to detect the taps which could be anywhere on iPhone not just iPhone screen. Here is a link which shows that it is possible.
基本上我想要做的是发送警报,如果用户在iPhone上轻拍3次而电话放在口袋里。
我所取得的成就是我可以检测到3个水龙头,但在这些情况下我也会得到错误警报。 1)如果用户走路,2)挥动手机3)跑步。我需要检查一下用户是否已经打了3次iPhone。
Basically what i want to do is send an alert if user taps 3 times on iPhone while the Phone is in his pocket. What i have achieved is that i can detect the 3 taps but i also get the false alerts as well in these cases. 1) if user walking, 2) waving his phone 3) running. I need to just check if user has hit his iPhone 3 times.
这是我的代码。
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
if (handModeOn == NO)
{
if(pocketFlag == NO)
return;
}
float accelZ = 0.0;
float accelX = 0.0;
float accelY = 0.0;
accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));
self.z.text = [NSString stringWithFormat:@"%0.1f", -accelZ];
if((-accelZ >= [senstivity floatValue] && timerFlag) || (-accelZ <= -[senstivity floatValue] && timerFlag)|| (-accelX >= [senstivity floatValue] && timerFlag) || (-accelX <= -[senstivity floatValue] && timerFlag) || (-accelY >= [senstivity floatValue] && timerFlag) || (-accelY <= -[senstivity floatValue] && timerFlag))
{
timerFlag = false;
addValueFlag = true;
timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
}
if(addValueFlag)
{
if (self.xSwitch.on)
{
NSLog(@"X sWitch is on");
[self.accArray addObject:[NSNumber numberWithFloat:-accelX]];
}
if (self.ySwitch.on)
{
NSLog(@"Y Switch is on");
[self.accArray addObject:[NSNumber numberWithFloat:-accelY]];
}
if (self.zSwitch.on)
{
NSLog(@"Z Switch is on");
[self.accArray addObject:[NSNumber numberWithFloat:-accelZ]];
}
}
//}
}
- (void)timerTick:(NSTimer *)timer1
{
[timer1 invalidate];
addValueFlag = false;
int count = 0;
for(int i = 0; i < self.accArray.count; i++)
{
if(([[self.accArray objectAtIndex:i] floatValue] >= [senstivity floatValue]) || ([[self.accArray objectAtIndex:i] floatValue] <= -[senstivity floatValue]))
{
count++;
[self playAlarm:@"beep-1" FileType:@"mp3"];
}
if(count >= 3)
{
[self playAlarm:@"06_Alarm___Auto___Rapid_Beeping_1" FileType:@"caf"];
[self showAlert];
timerFlag = true;
[self.accArray removeAllObjects];
return;
}
}
[self.accArray removeAllObjects];
timerFlag = true;
}
我们非常感谢任何帮助。
Any help will be really appreciated.
谢谢
推荐答案
以下是我如何实现它。
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
if (pause)
{
return;
}
if (handModeOn == NO)
{
if(pocketFlag == NO)
return;
}
// float accelZ = 0.0;
// float accelX = 0.0;
// float accelY = 0.0;
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
float accelX = acceleration.x - rollingX;
float accelY = acceleration.y - rollingY;
float accelZ = acceleration.z - rollingZ;
if((-accelZ >= [senstivity floatValue] && timerFlag) || (-accelZ <= -[senstivity floatValue] && timerFlag)|| (-accelX >= [senstivity floatValue] && timerFlag) || (-accelX <= -[senstivity floatValue] && timerFlag) || (-accelY >= [senstivity floatValue] && timerFlag) || (-accelY <= -[senstivity floatValue] && timerFlag))
{
timerFlag = false;
addValueFlag = true;
timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
}
if(addValueFlag)
{
[self.accArray addObject:[NSNumber numberWithFloat:-accelX]];
[self.accArray addObject:[NSNumber numberWithFloat:-accelY]];
[self.accArray addObject:[NSNumber numberWithFloat:-accelZ]];
}
}
这篇关于通过加速度计检测iPhone上任何位置的硬敲击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!