Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gesture recognizer not working for some reason, on iphone

This is in a UILabel subclass I made, but it doesn't seem to work. It doesn't call "tappedOn" for some reason.

gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOn)];
[self addGestureRecognizer:gesture];
[self setUserInteractionEnabled:YES];

Do you know why it might be?

Here is the tappedOn method:

-(void)tappedOn
{

NSLog(@"tapped");
if ([self.type isEqualToString: @"load"])
{
    [self.manager.manager loadSavedGameInRow:self.name];
}

if ([self.type isEqualToString: @"save"])
{
    [self.manager.manager saveGameInRow:self.name];
}
}
like image 559
Horton Jackson Avatar asked Sep 20 '13 11:09

Horton Jackson


4 Answers

For me it works:

first set this : [self setUserInteractionEnabled:YES];

then:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(method:)];
tap.numberOfTapsRequired = 1;

[self addGestureRecognizer:tap];
like image 74
Irfa Avatar answered Oct 18 '22 01:10

Irfa


I tried this code for my project and its working fine for me.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureUpdated:)];
    tapGesture.delegate = self;
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    tapGesture.cancelsTouchesInView = NO;
    [[self view] addGestureRecognizer:tapGesture];

- (IBAction)tapGestureUpdated:(UIGestureRecognizer *)recognizer
{
    // Code to respond to gesture here
}
like image 20
Manthan Avatar answered Oct 18 '22 01:10

Manthan


In the off chance that your label is outside the bounds of its superview, touch handling will likely not work. Worth a check, since your gesture recognizer code seems correct.

One more thing to try: are you sure your code to add the gesture recognizer to the view is being run? What method are you in when you call it?

like image 4
Zev Eisenberg Avatar answered Oct 18 '22 03:10

Zev Eisenberg


I found a good example for you: stackoverflow link

This is a step by step guide on how to implement gesture recognizers in your class:

Conform your class to UIGestureRecognizerDelegate protocol.

Instantiate the gesture recognizer. For example, to instantiate a UITapGestureRecognizer, we will do:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];

Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:

- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
    //Code to handle the gesture
}

The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like, UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded, etc.

Set the desired properties on the instantiated gesture recognizer. For example, for a UITapGestureRecognizer, we can set the properties numberOfTapsRequired, and numberOfTouchesRequired.

Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:

[self.imageView addGestureRecognizer:tapGestureRecognizer];

After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:

tapGestureRecognizer.delegate = self;

Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won’t be called.

like image 2
incmiko Avatar answered Oct 18 '22 02:10

incmiko