0

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

5 回答 5

2

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

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

它应该是

self.tapGestureRecognizer2.numberOfTapsRequired = 2;

希望这会帮助你。

于 2013-09-03T11:07:59.133 回答
0

看起来您的逻辑在下面的粗体突出显示代码行中存在问题!

-(void)viewDidLoad { [超级 viewDidLoad];

self.view.backgroundColor = [UIColor greenColor];

// 创建点击手势识别器

//==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 需要GestureRecognizerToFail:self.tapGestureRecognizer2]; [self.tapGestureRecognizer1 需要GestureRecognizerToFail:self.tapGestureRecognizer3]; [self.tapGestureRecognizer2 需要GestureRecognizerToFail:self.tapGestureRecognizer3];

// 添加手势 [self.view addGestureRecognizer:self.tapGestureRecognizer1]; [self.view addGestureRecognizer:self.tapGestureRecognizer2]; [self.view addGestureRecognizer:self.tapGestureRecognizer3];

}

于 2013-09-03T11:30:34.940 回答
0

尝试这个

注释此语句,该语句出现在您的代码中的三个位置。

self.tapGestureRecognizer1.numberOfTouchesRequired = 1;

又一题,换

self.tapGestureRecognizer3.numberOfTapsRequired = 2;

self.tapGestureRecognizer2.numberOfTapsRequired = 2;

因为 tapGestureRecognizer2 需要的点击数等于 2

于 2013-09-03T11:19:27.130 回答
0

以编程方式(并以某种风格)进行。

UILongPressGestureRecognizer *longPress;
int idx;

for (idx = 1; idx <= 3; idx++) {
    longPress = [[UILongPressGestureRecognizer alloc]
                                           initWithTarget:self action:@selector(buttonLongPressed:)];
    longPress.minimumPressDuration = min_press_duration;
    longPress.numberOfTapsRequired = 0;
    longPress.numberOfTouchesRequired = idx;
    [button addGestureRecognizer:longPress];
    [longPress release];
}
于 2016-01-15T20:15:26.363 回答
-1

添加[NSObject cancelPreviousPerformRequestsWithTarget:self];到 handleDoubleTap 和 handlelTripleTap 方法。这将取消先前操作的执行。

于 2013-09-03T11:38:31.653 回答