swift - 带有 UITapGestureRecognizer 的 UIView 通过拖动传递到它下面的 UITableView?

标签 swift uitableview uitapgesturerecognizer

我有一个如下所示的 View 层次结构:

* Root UIView
  * UITableView
  * UIView

UIView 位于“顶部”,因此它遮挡了部分 UITableView

UIView 上还有一个 UITapGestureRecognizer 允许点击它。

我想要的是让 UIView 处理单击,但将拖动事件向下传递给 UITableView(以便 UITableView 可以滚动)。点击工作正常,但 UIView 吞没了拖动事件。

我已经设置了 cancelsTouchesInView 但它没有帮助。

可以在 https://github.com/SuperTango/TapGestureOnTopOfTableView 找到下图的 repo

这是一张 gif 图像,展示了我在说什么。

enter image description here

最佳答案

您可以通过将 UIPanGestureRecognizer 添加到您的 TappableView 来做到这一点。 我是怎么做到的:

在您的 ViewController 中,我添加了一个 TappableView 的 IBoutlet:

@IBOutlet weak var pinkView: TappableView!

在 TappableView 中我添加了你的 tableView 的弱引用:

 weak var tableView:UITableView?

在您的 ViewController 的 viewDidLoad 中,我们设置了 TappableView 的表引用:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.dataSource = self

    pinkView.tableView = self.tableView
}

在 TappableView 中,我像这样添加一个 UIPanGestureRecognizer :

 // Here we add the PanGesture
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(_:)))
    self.addGestureRecognizer(panGestureRecognizer)

最后我像这样实现平移手势的 Action :

// Handle Pan gesture action
func handlePanGesture(_ panGesture: UIPanGestureRecognizer) {
    let translation = panGesture.translation(in: self)
    panGesture.setTranslation(CGPoint.zero, in: self)

    if let tableView = self.tableView{
        tableView.contentOffset = CGPoint(x: tableView.contentOffset.x, y: tableView.contentOffset.y + translation.y)
    }

}

TappableView完整代码

import UIKit

class TappableView: UIView {

weak var tableView:UITableView?
override init(frame: CGRect) {
    super.init(frame: frame)
    self.setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.setup()
}

func setup() {
    let singleTapRecognizer = UITapGestureRecognizer()
    singleTapRecognizer.addTarget(self, action: #selector(self.tappedHandler(_:)))
    singleTapRecognizer.numberOfTapsRequired = 1
    singleTapRecognizer.cancelsTouchesInView = false
    self.addGestureRecognizer(singleTapRecognizer)

    // Here we add the PanGesture
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(_:)))
    self.addGestureRecognizer(panGestureRecognizer)
}

func tappedHandler(_ sender: UITapGestureRecognizer) {
    NSLog("Got a tap")
}


// Handle Pan gesture action
func handlePanGesture(_ panGesture: UIPanGestureRecognizer) {
    let translation = panGesture.translation(in: self)
    panGesture.setTranslation(CGPoint.zero, in: self)

    if let tableView = self.tableView{
        tableView.contentOffset = CGPoint(x: tableView.contentOffset.x, y: tableView.contentOffset.y + translation.y)
    }

}


}

ViewController完整代码

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var pinkView: TappableView!
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.dataSource = self
    pinkView.tableView = self.tableView
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 50
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel!.text = String(indexPath.row)
    return cell
}
}

如果这能解决您的问题,请告诉我

关于swift - 带有 UITapGestureRecognizer 的 UIView 通过拖动传递到它下面的 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975352/

相关文章:

swift - Firebase & swift 使用.indexOn 对数据快照进行排序

ios - 在 UITableViewCell 中复制 UITextField

ios - 如何在 iOS TableView 中每行创建多个点击处理程序?

iOS:如何从 Swift 类触发 Obj-C 中的 UITapGestureRecognizer 和 UIPanGestureRecognizer?

ios - 在 Swift 中将图层样式组合到多个 UIButton

swift - AVAudioPlayer 在 Swift 4.2 中产生延迟

ios - Swift 2.0 随机 EXC_BAD_ACCESS

swift - 将变量从 customCell.xib 的按钮传递到另一个 UIViewController

ios - 如何编辑 Realm 对象并保存编辑?

ios - 将 UITapGestureRecognizer 添加到 UILabel 特定部分的最佳方法是什么?