How to add Swipe Gestures to UITableView cell?
Asked Answered
C

3

13

I added this code in cellForRowAtIndexPath

UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(handleSwipeFrom:)];
        [gestureR setDirection:UISwipeGestureRecognizerDirectionRight];//|UISwipeGestureRecognizerDirectionRight)];
        [cell addGestureRecognizer:gestureR];

it works fine. But I want UISwipeGestureRecognizerDirectionLeft so Added like this

[gestureR setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)];

When I check with direction and state I am always getting 3 = 3

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {    

    NSLog(@"%d = %d",recognizer.direction,recognizer.state);
}

if I apply only one Gesture it works fine. I tried to add two gestures one by one. but it will responding for only one gesture.

How to add second gestures. I added directly to one gesture to TableView another one to cell but now use.

Coprophilous answered 19/12, 2011 at 16:16 Comment(0)
N
23

Try this

UISwipeGestureRecognizer* gestureR;
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:gestureR];

gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
[view addGestureRecognizer:gestureR];

If you want to handle different functionalities on left and right swipes, just change the selectors.

Nicholenicholl answered 19/12, 2011 at 16:24 Comment(0)
U
7

Instead of two times alloc, it would be better if you use

UISwipeGestureRecognizer* recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft+UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:recognizer];

And get the direction of swipe in the action as :

-(void)handleSwipe:(UISwipeGestureRecognizer *) sender 
{
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) 
    {
    //do something
    }
    else //if (sender.direction == UISwipeGestureRecognizerDirectionRight) 
    {
  //do something
     }
}
Unbearable answered 30/11, 2013 at 7:32 Comment(0)
B
5

I know its been ages since you asked this. But try and read following line again in your question. [gestureR setDirection:UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionRight)];

Did you realise you added UISwipeGestureRecognizerDirectionRight. Twice!!

:D

Bili answered 9/5, 2013 at 6:42 Comment(1)
Thanks. For the just record [gestureR setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)]; Works great. Tried and tested on Xcode 4.6.2Bili

© 2022 - 2024 — McMap. All rights reserved.