touchesBegan(:) 无法在 tableview/scrollview 上工作

touchesBegan(:) not working on tableview/scrollview

有一些SO post但与我的情况无关

touchesBegan(:) 不处理 tableview/scrollview. UITableViewUIScrollView 会消耗所有触摸事件,并且不会传递给下一个响应者。我有一个基本视图控制器,我想将触摸事件接收到基本控制器中 touchesBegan(:). 在某些子视图控制器(扩展基本视图控制器)中,我使用了 tableView。当我触摸 table 查看它的消耗时,基本控制器中没有收到任何事件 touchesBegan(:). 如何处理?

我希望首先接收到基本控制器 touchesBegan(:) 的触摸。然后有条件地,我想允许子视图控制器,table视图是否使用它。

import UIKit

class ItemCell: UITableViewCell {

    @IBOutlet weak var title: UILabel!
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("Touches began!")
    }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell =  tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
        cell.title?.text = "Click me"
        return cell
    }
}

在上面的代码中,当我触摸 table 之外的视图时,将调用 print("Touches began!")。但是当我触摸 table 内部时,我仍然想触发打印 ("Touches began!")。让我们假设 tableview 占总浏览量的一半。

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    <#code#>
}

以上方法用于检测触摸。
如果你想检测你的tableview的触摸,那么首先创建一个tableview的subclass,然后将你的tableview分配给这个class,然后将上面的方法写入tableview subclass。在 tableview subclass 中,您可以检测到触摸,上面的方法将被调用。

仅测试 tblview.isUserInteractionEnabled = false 并检查您的 viewcontroller touchesBegan 方法是否会调用。

更新您的 class 文件如下..

 class ItemCell: UITableViewCell {

    @IBOutlet weak var title: UILabel!
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, touchedTableviewTouchDelegate {

    @IBOutlet weak var tableView: touchedTableview!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.touchedTableviewdelegate = self
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("Touches began!")
    }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell =  tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
        cell.title?.text = "Click me"
        return cell
    }

    func touchesBegunInTableview(_ touches: Set<UITouch>, with event: UIEvent?) {
        //Here You can do any thing with touch
    }
}

protocol touchedTableviewTouchDelegate: class {
    func touchesBegunInTableview(_ touches: Set<UITouch>, with event: UIEvent?)
}

class touchedTableview: UITableView
{
    var touchedTableviewdelegate: touchedTableviewTouchDelegate?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        touchedTableviewdelegate?.touchesBegunInTableview(touches, with: event)
    }

}

我在我的代码库中遇到了同样的问题。 touches begin 对我不起作用,因为我们有动态 UI 组件,我无法为所有组件制定逻辑。所以我用了两个东西来解决这个问题

1.viewDidLoad() -> tableView.keyboardDismissMode = .Drag

2. on tableViewCell 选择方法 (didSelectRowForIndexPath) 放弃键盘。