Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Swipe Gestures to UITableView cell?

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.

like image 968
Naga Harish M Avatar asked Dec 19 '11 16:12

Naga Harish M


People also ask

What is Indexpath in Tableview Swift?

Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.

What is Uitableview in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


1 Answers

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.

like image 79
Neo Avatar answered Sep 28 '22 18:09

Neo