本文共 2900 字,大约阅读时间需要 9 分钟。
用Automatic Gesture Recognition实现:
在viewDidLoad中
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1)]; singleTap.numberOfTapsRequired = 1; singleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:singleTap]; UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2)]; doubleTap.numberOfTapsRequired = 2; doubleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:doubleTap]; [singleTap requireGestureRecognizerToFail:doubleTap]; UITapGestureRecognizer *tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap3)]; tripleTap.numberOfTapsRequired = 3; tripleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:tripleTap]; [doubleTap requireGestureRecognizerToFail:tripleTap]; UITapGestureRecognizer *quadrupleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap4)]; quadrupleTap.numberOfTapsRequired = 4; quadrupleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:quadrupleTap]; [tripleTap requireGestureRecognizerToFail:quadrupleTap]; }
添加自定义响应方法:
- (void)tap1 { singleLabel.text = @"Single Tap Detected"; [self performSelector:@selector(eraseMe:) withObject:singleLabel afterDelay:1.6f]; } - (void)tap2 { doubleLabel.text = @"Double Tap Detected"; [self performSelector:@selector(eraseMe:) withObject:doubleLabel afterDelay:1.6f]; } - (void)tap3 { tripleLabel.text = @"Triple Tap Detected"; [self performSelector:@selector(eraseMe:) withObject:tripleLabel afterDelay:1.6f]; } - (void)tap4 { quadrupleLabel.text = @"Quadruple Tap Detected"; [self performSelector:@selector(eraseMe:) withObject:quadrupleLabel afterDelay:1.6f]; } - (void)eraseMe:(UILabel *)label { label.text = @""; }
其实也可以直接在touchesBegan中自己实现功能:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; NSUInteger tapCout = [touch tapCount]; switch (tapCout) { case 1: [self performSelector:@selector(tap1) withObject:nil afterDelay:.4];//延迟执行 break; case 2: [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap1) object:nil];//取消tap1 [self performSelector:@selector(tap2) withObject:nil afterDelay:.4]; break; case 3: [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap2) object:nil];//取消tap2 [self performSelector:@selector(tap3) withObject:nil afterDelay:.4]; break; case 4: [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tap3) object:nil];//取消tap3 [self tap4]; break; default: break; } }
that‘s all.
本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/03/26/2417513.html,如需转载请自行联系原作者