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.
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.
Here is my code.
- (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.
Thanks