Bhavin Ramani
Bhavin Ramani

Reputation: 3219

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

Upvotes: 1

Views: 1445

Answers (3)

Ryan
Ryan

Reputation: 1802

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

override func awakeFromNib() {
    super.awakeFromNib()
   // add gesture here
}

Upvotes: 0

Anil solanki
Anil solanki

Reputation: 962

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];

Upvotes: 1

Jogendra.Com
Jogendra.Com

Reputation: 6454

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

Upvotes: 0

Related Questions