1

Is there a way to detect up & down swipes on TableView in a ViewController?

in ViewDidLoad();

    let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeUp:"))
    let downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swipeDown:"))

    upSwipe.direction = .Up
    downSwipe.direction = .Down

    tableView.addGestureRecognizer(upSwipe)
    tableView.addGestureRecognizer(downSwipe)

Unfortunately, this doesn't work.

func swipeDown() {
    print("A")     
}

nothing returned.

What is the appropriate way to detect gestures on TableView (not cell/as a whole)?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
senty
  • 12,385
  • 28
  • 130
  • 260
  • Possible duplicate of [UITableView Scroll event](http://stackoverflow.com/questions/8642699/uitableview-scroll-event) – BLE Apr 09 '16 at 01:30
  • @BLE I was asking for Swift – senty Apr 09 '16 at 01:31
  • Apologies, but it's mostly semantics. The information you're looking for is in the UIScrollViewDelegate portion of the documentation on developer.apple.com `scrollViewDidScroll(_:)` – BLE Apr 09 '16 at 01:37
  • @BLE I tried [this answer](http://stackoverflow.com/a/24166783/4705339) but couldn't make it work :/ What am I doing wrong? – senty Apr 09 '16 at 01:46
  • What about it didn't work? Did you put anything inside `func scrollViewDidScroll(scrollView: UIScrollView!) { }` ? – BLE Apr 09 '16 at 01:49
  • It doesn't let me use `super.init(nibName:UITableViewCellStyle.Default, bundle:"MyCell")`.. It doesn't let me bypass this line.. So I can't build – senty Apr 09 '16 at 01:58

1 Answers1

2

Implement UIScrollViewDelegate

Define scrollViewDidScroll(_:)

Set your tableview's scrollview's delegate to whatever object you've got the above defined in.

BLE
  • 161
  • 7
  • This line crashes `super.init(nibName:UITableViewCellStyle.Default, bundle:"MyCell")`` for some reason. I setup `var scrollView: UIScrollView`, added Delegate and then included `scrollViewDidScroll` & `scrollViewWillEndDragging`.. The init part still crashes.. I don't know if it matters but, I have TableView, not TableViewController – senty Apr 09 '16 at 01:49
  • presumably your code in your original question was written in UITableViewController, no? – BLE Apr 09 '16 at 01:58
  • No, it's a TableView inside ViewController. `class TrialViewController: UIViewController, UITableViewDataSource, UITableViewDelegate`; so I tried adding `UIScrollViewDelegate`, but at this point, it needed initialisers. Using the `init` in [this answer](http://stackoverflow.com/a/24166783/4705339) gives error. – senty Apr 09 '16 at 01:59
  • Okay, I get it now. All I needed was to add `scrollViewDidScroll` function and no delegate. Ahh.... Thanks – senty Apr 09 '16 at 02:42