【问题标题】:UIView with UITapGestureRecognizer pass through drag to a UITableView beneath it?带有 UITapGestureRecognizer 的 UIView 通过拖动到它下面的 UITableView ?
【发布时间】:2017-02-01 08:45:42
【问题描述】:

我的视图层次结构如下所示:

* Root UIView
  * UITableView
  * UIView

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

UIView 上也有一个UITapGestureRecognizer,它可以被窃听。

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

我设置了cancelsTouchesInView,但没有帮助。

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

这是一个 gif,显示我在说什么。

【问题讨论】:

标签: swift uitableview uitapgesturerecognizer


【解决方案1】:

您可以通过将 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)

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

// 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
}
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多