Question

I have a small problem that em stuck on.. i have a Custom UITableViewCell, on its textView i have added 2 gestures, UITapGesture and UISwipeGesture.. the tap gesture is working fine but the swipe gesture is calling the method multiple times.. some times calling it twice and some times even more than that… Here's how i have added them to the cell

//added in cellForRowAtIndexPath Method
UITapGestureRecognizer *tapToTranslate = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToTranslate:)];
[tapToTranslate setNumberOfTapsRequired:1];
[tapToTranslate setNumberOfTouchesRequired:1];
tapToTranslate.delegate = self;
[cell.messageContentView addGestureRecognizer:tapToTranslate];


UISwipeGestureRecognizer *swipeToTranslate = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(translateTo:)];
swipeToTranslate.numberOfTouchesRequired = 1;
swipeToTranslate.direction = UISwipeGestureRecognizerDirectionLeft;
swipeToTranslate.delegate = self;
[cell.messageContentView addGestureRecognizer: swipeToTranslate];

These are there methods…

-(void)tapToTranslate:(UITapGestureRecognizer *)aGesture {}

-(void)translateTo:(UISwipeGestureRecognizer *)aGesture 
{
    aGesture.enabled = false;
}

I've tried to disable Swipe Gesture in its method after its called but that didn't help..

I've also have the uigesturerecognizer delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

So Any help would be great… thanks in advance…

EDIT 1

<UITextView: 0x11322f700; frame = (18 10; 160.865 69.2656); text = '你怎么样?    How are you doing?'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x11322fbd0>; layer = <CALayer: 0x11322fac0>; contentOffset: {0, 0}>
Was it helpful?

Solution 2

After Removing the gesture.delegate = self; line from both the gestures, the method started calling once as it was suppose to do. Apparently doing the job for me. So for anyone facing this issue, they can try removing the gesture's delegate and its method.

OTHER TIPS

The UISwipeGestureRecognizer does call the function multiple times for different states like UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded, and several other states. It also keeps on calling the function constantly while it is swiping which can be handled in the last else statement below. In the swipe gesture function, do this:

-(void)translateTo:(UISwipeGestureRecognizer *)aGesture
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
//do something
} 
else if(recognizer.state==UIGestureRecognizerStateEnded)
{
}
else
{
 //do something while it is swiping
}
}

The below answer may not be in correspondence to what you intend to do but still might help you: UISwipeGestureRecognizer called twice

The thing is, Tableview is having its own gesture recognizers. By adding additional gestures over the tableview it is somehow confusing with the gesture to call. This might be the reason of this issue. Here is the solution;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if ([gestureRecognizer.view isKindOfClass:[UITableView class]]) {
       ...
    } else {
       ...
    }
}

You can recognize simultaneous gestures using this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top