我正在使用UITapGestureRecognizer
并试图让不同的水龙头在同一个 ViewController 上工作,但一键点击失败。
我试图找到一个解决方案,但无法将其集成到我的测试代码中。我非常感谢一些帮助,以便我可以从中学习。
这是我用来测试的代码:
.h 文件:
@interface ViewController : UIViewController <UIGestureRecognizerDelegate>
.m 文件:
@interface ViewController ()
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer3;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer2;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer1;
@end
@implementation ViewController
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
// Create Tap Gesture Recognizer
//==1
self.tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle1Taps:)];
self.tapGestureRecognizer1.numberOfTouchesRequired = 1;
self.tapGestureRecognizer1.numberOfTapsRequired = 1;
//==2
self.tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle2Taps:)];
self.tapGestureRecognizer2.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 2;
//== #3
self.tapGestureRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle3Taps:)];
self.tapGestureRecognizer3.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 3;
[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer2];
[self.tapGestureRecognizer1 requireGestureRecognizerToFail:self.tapGestureRecognizer3];
[self.tapGestureRecognizer2 requireGestureRecognizerToFail:self.tapGestureRecognizer3];
// Add gestures
[self.view addGestureRecognizer:self.tapGestureRecognizer1];
[self.view addGestureRecognizer:self.tapGestureRecognizer2];
[self.view addGestureRecognizer:self.tapGestureRecognizer3];
}
- (void)handle1Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-1-Taps");
NSUInteger touchCounter = 0;
for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {
CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];
NSLog(@">>1 x TAP<<");
}
}
- (void)handle2Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-2-Taps");
NSUInteger touchCounter = 0;
for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {
CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];
NSLog(@">>2 x TAP<<");
}
}
- (void)handle3Taps:(UITapGestureRecognizer *)paramSender {
NSLog(@"handle-3-Taps");
NSUInteger touchCounter = 0;
for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++) {
CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];
NSLog(@">>3 x TAP<<");
}
}
@end
以下是与VC第一次交互的结果:
- 1 x tap = >>2 x TAP<<
- 2 x tap = >>2 x TAP<<
- 3 x tap = >>3 x TAP<<