ahmedlabib ahmedlabib - 6 months ago 14
iOS Question

UITableViewCell How can I detect that the user has swiped left or Right?

I am using SWTableViewCell and I want to know the action that is triggered when the user swipe Left or Right.

Answer

you have to add Gesture Recognizer in you cellForRowAtIndexPath

 UISwipeGestureRecognizer* swRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)];
[swRight setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:swRight];

UISwipeGestureRecognizer* swLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)];
[swLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[cell addGestureRecognizer:swLeft];

and then its selector method

-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // your code
    }
}

-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // your code
    }
}
Comments