【问题标题】:Detecting Swipe Gestures on TableView检测 TableView 上的滑动手势
【发布时间】:2016-07-30 10:21:59
【问题描述】:

有没有办法在 ViewController 中检测 TableView 上的上下滑动?

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)

很遗憾,这不起作用。

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

没有返回。

在 TableView(不是单元格/作为一个整体)上检测手势的适当方法是什么?

【问题讨论】:

  • UITableView Scroll event的可能重复
  • @BLE 我要的是 Swift
  • 道歉,但这主要是语义上的。您要查找的信息位于 developer.apple.com scrollViewDidScroll(_:) 上文档的 UIScrollViewDelegate 部分
  • @BLE 我尝试了this answer,但无法成功:/我做错了什么?
  • 没有用怎么办?你在func scrollViewDidScroll(scrollView: UIScrollView!) { } 里面放了什么吗?

标签: ios swift uitableview uigesturerecognizer


【解决方案1】:

实现 UIScrollViewDelegate

定义scrollViewDidScroll(_:)

将您的 tableview 的滚动视图的委托设置为您在上面定义的任何对象。

【讨论】:

  • 这条线崩溃super.init(nibName:UITableViewCellStyle.Default, bundle:"MyCell")`` for some reason. I setup var scrollView: UIScrollView, added Delegate and then included scrollViewDidScroll` & scrollViewWillEndDragging.. init 部分仍然崩溃.. 我不知道这是否重要但是,我有 TableView,不是 TableViewController
  • 大概你原来问题中的代码是用 UITableViewController 写的吧?
  • 不,它是 ViewController 中的 TableView。 class TrialViewController: UIViewController, UITableViewDataSource, UITableViewDelegate;所以我尝试添加UIScrollViewDelegate,但此时,它需要初始化程序。在this answer 中使用init 会出错。
  • 好的,我现在明白了。我所需要的只是添加scrollViewDidScroll 函数并且没有委托。啊……谢谢
猜你喜欢
  • 2017-07-01
  • 1970-01-01
  • 2012-08-28
  • 2017-10-18
  • 1970-01-01
  • 2020-01-17
相关资源
最近更新 更多