Chandu
Chandu

Reputation: 630

iOS TapGesture not working

Initially I had one label and added a gesture recognizer. It was working. I added the same gesture recogniser to another label, then it stopped working on the first label and continued working on the second label, that too very inconsistently. Sometimes it works on first label and sometimes on second. Even the behaviour is same on device. Any thoughts please.

The view controller implements the UIGestureRecognizerDelegate protocol.

label1.tag=8;
label2.tag=9;

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(foundRecognizer:)];

tapRecognizer.numberOfTouchesRequired=1;
tapRecognizer.numberOfTapsRequired=1;
[tapRecognizer setDelegate:self];

label1.userInteractionEnabled=YES;
label2.userInteractionEnabled=YES;

[label1 addGestureRecognizer:tapRecognizer];
[label2 addGestureRecognizer:tapRecognizer];

Upvotes: 0

Views: 63

Answers (1)

Reinhard Männer
Reinhard Männer

Reputation: 15217

The docs say "A gesture recognizer operates on touches hit-tested to a specific view and all of that view’s subviews. It thus must be associated with that view." I think it is therefore not possible to associate one gesture recognizer with more than 1 view.
I would thus create 2 gesture recognizers and assign one of them each to your labels.

Upvotes: 1

Related Questions