Lolloz89
Lolloz89

Reputation: 2809

Double tap selector fired multiple times

EDIT: I have this methods in an UIView placed on the top of multiple view controllers which are the "data source" of a UIPageViewController

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];

        UITapGestureRecognizer *tapGR =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
        [tapGR setDelegate:self];
        [tapGR setNumberOfTapsRequired:1];
        [self addGestureRecognizer:tapGR];


        UITapGestureRecognizer *doubleTapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
        [doubleTapGR setNumberOfTapsRequired:2];
        [self addGestureRecognizer:doubleTapGR];

        [tapGR requireGestureRecognizerToFail :doubleTapGR];

        [tapGR release];
        [doubleTapGR release];
    }
    return self;
}

-(void)handleTap:(UITapGestureRecognizer *)tapRecognizer{
    if (!(tapRecognizer.state == UIGestureRecognizerStatePossible)) {
        [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:kTapOnCenterNotificationName object:nil]];
    }
}

-(void)handleDoubleTap:(UITapGestureRecognizer *)doubleTapRecognizer{

    CGPoint point = [doubleTapRecognizer locationInView:self];

    LSSharedVariables *sharedVariables = [LSSharedVariables sharedInstance];
    [sharedVariables setDoubleTapPoint:point];

    [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:kLongPressNotificationName object:nil]];

}

Even if I specified that the actions should be performed only if the gesture recognizer state is equal to UIGestureRecognizerStateEnded, the log is shown multiple times. I'd like to perform actions in this block only once, how should I do this?

EDIT: I figured out that the double tap method is called many times as much are the pages of the UIPageViewController. The thing I can't understand is why it isn't the same for the singleTapGestureRecognizer.

Upvotes: 1

Views: 676

Answers (2)

George Clingerman
George Clingerman

Reputation: 1395

It sounds like you might have multiple objects listening for touches and all calling "handleDoubleTap" independently when it happens. That would explain why you're seeing it called multiple times when the double-tap event occurs.

I would start by searching through your code to verify that you're not adding it multiple times since the behavior you're experiencing isn't standard behavior for the touch events.

Upvotes: 0

mshau
mshau

Reputation: 503

- (void) handleDoubleTap : (UIGestureRecognizer*) sender
{
NSLog (@"Double tap is being handled here");
}

- (void) handleSingleTap : (UIGestureRecognizer*) sender
{
NSLog (@"Single tap is being handled here");
}

- (void) loadView
{
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleSingleTap];

[singleTap requireGestureRecognizerToFail : doubleTap];

[doubleTap setDelaysTouchesBegan : YES];
[singleTap setDelaysTouchesBegan : YES];

[doubleTap setNumberOfTapsRequired : 2];
[singleTap setNumberOfTapsRequired : 1];

[self.view addGestureRecognizer : doubleTap];
[self.view addGestureRecognizer : singleTap];

[singleTap release];
[doubleTap release];
}

try this

Upvotes: 1

Related Questions