1 Preface
Use UITapGestureRecognizer to listen to events that capture users' clicks on the screen.
2. code example
ZYViewController. m
[Plain]
(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Create a gesture
Self. tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (handleTaps :)];
/* The screen must have two fingers */
Self. tapGestureRecognizer. numberOfTouchesRequired = 2;
/* You must press 3 */
Self. tapGestureRecognizer. numberOfTapsRequired = 3;
[Self. view addGestureRecognizer: self. tapGestureRecognizer];
}
-(Void) handleTaps :( UITapGestureRecognizer *) paramSender {
NSUInteger touchCounter = 0;
For (touchCounter = 0; touchCounter <paramSender. numberOfTouchesRequired; touchCounter ++ ){
// Obtain the finger position
CGPoint touchPoint = [paramSender locationOfTouch: touchCounter inView: paramSender. view];
NSLog (@ "Touch # % lu: % @", (unsigned long) touchCounter + 1, NSStringFromCGPoint (touchPoint ));
}
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Create a gesture
Self. tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (handleTaps :)];
/* The screen must have two fingers */
Self. tapGestureRecognizer. numberOfTouchesRequired = 2;
/* You must press 3 */
Self. tapGestureRecognizer. numberOfTapsRequired = 3;
[Self. view addGestureRecognizer: self. tapGestureRecognizer];
}
-(Void) handleTaps :( UITapGestureRecognizer *) paramSender {
NSUInteger touchCounter = 0;
For (touchCounter = 0; touchCounter <paramSender. numberOfTouchesRequired; touchCounter ++ ){
// Obtain the finger position
CGPoint touchPoint = [paramSender locationOfTouch: touchCounter inView: paramSender. view];
NSLog (@ "Touch # % lu: % @", (unsigned long) touchCounter + 1, NSStringFromCGPoint (touchPoint ));
}
}
Running result
The two fingers are displayed on the console three times in a row after the screen is clicked.
15:58:36. 524 UITapGestureTest [4482: c07] Touch #1: {180,321}
15:58:36. 525 UITapGestureTest [4482: c07] Touch #2: {140,119}