How to add Swipe Gestures to UITableView cell?

前端 未结 3 1246
别那么骄傲
别那么骄傲 2020-12-14 03:28

I added this code in cellForRowAtIndexPath

UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
                                       


        
3条回答
  •  没有蜡笔的小新
    2020-12-14 04:32

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

提交回复
热议问题