How to add Swipe Gestures to UITableView cell?

醉酒当歌 提交于 2019-11-28 19:41:37

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.

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
     }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!