1

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}>
Ahmed Z.
  • 2,329
  • 23
  • 52

3 Answers3

2

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

Community
  • 1
  • 1
Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
  • its always calling UIGestureRecognizerStateEnded.. never going in Began or else... – Ahmed Z. Apr 17 '14 at 07:10
  • 1
    swipe is a discrete gesture – KudoCC Apr 17 '14 at 07:38
  • "swipe is a discrete gesture" means? – Ahmed Z. Apr 17 '14 at 07:40
  • @AhmedZ. There are two types of gesture, one is discrete, the other is continuous. Discrete gesture don't have begin state. – KudoCC Apr 17 '14 at 07:45
  • oh, thanks for sharing that… :) do u know why me method is called multiple times? – Ahmed Z. Apr 17 '14 at 07:48
  • 1
    @KudoCC Discrete gestures do not mean that they don't have a began state. Swipe gestures does have a began state and it gets called. – Khawar Ali Apr 17 '14 at 07:52
  • Discrete has three states, possible, fail, or recognized. Check it in the [link](https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html#//apple_ref/doc/uid/TP40009541-CH2-SW17) – KudoCC Apr 17 '14 at 07:57
  • @KhawarAli Because "**Swipe** is a **discrete gesture**" – Ahmed Z. May 05 '14 at 11:54
  • @AhmedZ. I also said that it is a discrete gesture, but being discrete does not mean that the began state does not get called. It just means that the functions is not called repeatedly. The began state does get called in the swipe gesture! – Khawar Ali May 05 '14 at 12:12
1

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.

Lachlan
  • 312
  • 2
  • 15
Ahmed Z.
  • 2,329
  • 23
  • 52
0

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.

Mohit Manhas
  • 3,471
  • 1
  • 17
  • 21