How to add Swipe Gestures to UITableView cell?

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

I added this code in cellForRowAtIndexPath

UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
                                       


        
相关标签:
3条回答
  • 2020-12-14 04:29

    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

    0 讨论(0)
  • 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
         }
    }
    
    0 讨论(0)
  • 2020-12-14 04:33

    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.

    0 讨论(0)
提交回复
热议问题