Swipe vertical gesture not working with UITableView
Asked Answered
B

3

5

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
}
}
Baneful answered 2/6, 2017 at 11:30 Comment(0)
P
19

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
}
Publias answered 2/6, 2017 at 11:40 Comment(2)
because in your tableview it works with scrollview and you are implementing swipe so there are two gesture.:) however if it's helpful then i hope you will accept my answer too. :)Publias
absolutely, just edit your answer and make the code for number #3 more appropriate !Baneful
F
3

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

Fretful answered 2/6, 2017 at 11:42 Comment(0)
K
2

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.

Kanarese answered 6/3, 2018 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.