A lot of the time, you can use buttons or tables to allow the user to interact with the UI of the iPhone.
What happens though, if you want the user to use the UITouch methods and have a double tap on the screen?
Well, this is incredibly easy to do!
Put the following code in your ViewController file, of the view that you want the user to double tap.
#pragma mark -
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
switch (tapCount) {
case 2:
//Put your code here
break;
default:
break;
}
}
If you look at the code, you will see that under the ‘case 2:’ you can put your code. This bit runs if the user has tapped the at least twice.
All this code does is to look for the number of taps that are received, the ’switch’ function can be edited to run code to whatever number of taps you want.
So, if you changed ‘case 2′ to ‘case 3′ the code would run after 3 taps.
However, there is a caveat here!!
You cannot use this code to run multiple separate tap counters. So if you wanted code to run after two taps and then a different bit of code to run after three taps, both sets of code would be run if the user entered three taps.
The code for two taps would run after two taps, regardless of how many more taps the user would enter!!
Of course, if you had something like ten taps, you could use it to hide easter eggs in your code!!
Any comments or questions are welcome, if you want to twitter, digg this or subscribe to the RSS Feed, please do!!
Happy coding!
Cheers
Graham