Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe vertical gesture not working with UITableView

I have UIViewController and I have added a UITableView to it in the storyboard, later I added a swipe Up gesture recognizer to the view, but nothing happened.

this is my code

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {

@IBOutlet weak var tableview: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let swipeRegongnizer = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipeUp))
    swipeRegongnizer.direction = UISwipeGestureRecognizerDirection.up
    swipeRegongnizer.delegate = self
    tableview.addGestureRecognizer(swipeRegongnizer)
}

func handleSwipeUp(gesture: UISwipeGestureRecognizer) {
        print("swiped up")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = "cell\(indexPath.row)"
    return cell
}
}
like image 891
MEH Avatar asked Jun 02 '17 11:06

MEH


3 Answers

1. Implement UIGestureRecognizerDelegate. 2. set delegate.

    yourGesture.delegate = self

3. add the following function in your respective swift file.

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
}
like image 127
elk_cloner Avatar answered Nov 15 '22 12:11

elk_cloner


Implement delegate method

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

This method will allow both the pan gesture of the table view and your swipe gesture to be recognized simultaneously

like image 45
Jože Ws Avatar answered Nov 15 '22 10:11

Jože Ws


You need to add the following delegate in your respective view controller.

Objective-C

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

Swift

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

Explanation

This method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer would block the other gesture recognizer from recognizing its gesture. Note that returning YES is guaranteed to allow simultaneous recognition; returning NO, on the other hand, is not guaranteed to prevent simultaneous recognition because the other gesture recognizer's delegate may return YES. By default it returns false.

like image 2
Deepraj Chowrasia Avatar answered Nov 15 '22 10:11

Deepraj Chowrasia