Search code examples
iosobjective-cuitableviewuiswipegesturerecognizer

UISwipeGestureRecognizerDirection only Recognizes Right Gesture


I am trying to add a left and right swipe gesture to every cell in my UITableView. However, no one has seemed to answer this question no matter where I look. Using this current solution, which is what is recommended, I do not enter the right gesture handler.

UISwipeGestureRecognizer* right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
right.direction = UISwipeGestureRecognizerDirectionRight;
[self.tableView addGestureRecognizer:right];


UISwipeGestureRecognizer* left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:left];

I create two different Gesture recognizers for my solution. However, I can never see a response from the right UIGestureRecognizer. I don't understand why I never see a response from the right gesture recognizer only left one.

-(void)swipeLeft:(UISwipeGestureRecognizer*)gestureRecognizer
{ NSLog("You have swiped Left");
}
-(void)swipeRight:(UISwipeGestureRecognizer*)gestureRecognizer
{ NSLog("You have swiped right"); //Never Enter this handler
}

Solution

  • Use this code...

    I hope you don't mind its in swift.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
        self.view.addGestureRecognizer(swipeRight)
    
        var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeDown.direction = UISwipeGestureRecognizerDirection.Down
        self.view.addGestureRecognizer(swipeDown)
    }
    
    func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
    
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                println("Swiped right")
            case UISwipeGestureRecognizerDirection.Down:
                println("Swiped down")
            default:
                break
            }
        }
    }