1

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
};
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41

3 Answers3

1

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];
Anil solanki
  • 962
  • 4
  • 20
0

Please add gesture delegate method in this

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

and this line with your gesture It will work

[yourgest setCancelsTouchesInView:NO];

It will work ..

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
0

I add the "add gesture code" in cell's awakeFromNib(),it works

override func awakeFromNib() {
    super.awakeFromNib()
   // add gesture here
}
Ryan
  • 1,802
  • 1
  • 9
  • 6