我正在使用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<<

最佳答案


检查一下:
最后一行是错误的。

self.tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle2Taps:)];
self.tapGestureRecognizer2.numberOfTouchesRequired = 1;
self.tapGestureRecognizer3.numberOfTapsRequired = 2;


它应该是

self.tapGestureRecognizer2.numberOfTapsRequired = 2;


希望这会帮助你。

关于iphone - 在同一View Controller中使一键,两键和三键工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18589834/

10-11 14:51