1 前言
使用UITapGestureRecognizer 監聽捕獲用戶點擊了屏幕的事件。
2 代碼實例
ZYViewController.m
[plain]
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
//創建手勢
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];
/* 屏幕上必須有兩個手指 */
self.tapGestureRecognizer.numberOfTouchesRequired = 2;
/* 必須敲擊3下 */
self.tapGestureRecognizer.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:self.tapGestureRecognizer];
}
- (void) handleTaps:(UITapGestureRecognizer*)paramSender{
NSUInteger touchCounter = 0;
for (touchCounter = 0;touchCounter < paramSender.numberOfTouchesRequired;touchCounter++){
//獲得手指位置
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];
//創建手勢
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];
/* 屏幕上必須有兩個手指 */
self.tapGestureRecognizer.numberOfTouchesRequired = 2;
/* 必須敲擊3下 */
self.tapGestureRecognizer.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:self.tapGestureRecognizer];
}
- (void) handleTaps:(UITapGestureRecognizer*)paramSender{
NSUInteger touchCounter = 0;
for (touchCounter = 0;touchCounter < paramSender.numberOfTouchesRequired;touchCounter++){
//獲得手指位置
CGPoint touchPoint =[paramSender locationOfTouch:touchCounter inView:paramSender.view];
NSLog(@"Touch #%lu: %@",(unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
}
}
運行結果
兩根手指連續三次點擊屏幕後控制台顯示
2013-05-13 15:58:36.524 UITapGestureTest[4482:c07] Touch #1: {180, 321}
2013-05-13 15:58:36.525 UITapGestureTest[4482:c07] Touch #2: {140, 119}