Search code examples
iosobjective-cuitableviewuiimageviewuiswipegesturerecognizer

Swipe gesture not working in UITableViewCell


I have UITableView in my ViewController. There is a custom UITableViewCell. In that custom cell I have a UIImageView and multiple UILabel. I want to add UISwipeGestureRecognizer on that UIImageView. I have take gesture programmatically but it is not working.

Here is the code which I have done:

////////// THIS CODE I TOOK IN CELL FOR ROW METHOD:
swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(Swipe_Handling:)];
swipe.direction=UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[cells.propimage addGestureRecognizer:swipe];
////////////


-(void)Swipe_Handling:(UISwipeGestureRecognizer *)recognizer
{
    if (recognizer.direction==UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"Right");
    }
    else if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft)
    {
        NSLog(@"Left");
    }
}

The swipe handling action is called but it is not going in if-else condition and cells.propimage.userInteractionEnabled=YES;.

When I print NSLog(@"%lu",(unsigned long)recognizer.direction); It returns me: 3

typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
   UISwipeGestureRecognizerDirectionRight = 1 << 0,
   UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
   UISwipeGestureRecognizerDirectionUp    = 1 << 2,
   UISwipeGestureRecognizerDirectionDown  = 1 << 3
};

Solution

  • Please try this code for 2 directions

    UISwipeGestureRecognizer* gestureR;
    gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
    gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
    [imgview addGestureRecognizer:gestureR];
    
    gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
    gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
    [imgview addGestureRecognizer:gestureR];