【问题标题】:SWIFT: touchesEnded not being called when I touch the screenSWIFT:触摸屏幕时未调用 touchesEnded
【发布时间】:2026-01-03 20:50:01
【问题描述】:

所以我有一个名为 SettingsViewController 的 tableviewController,它有以下 touchesEnded 函数:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("A")

    super.touchesEnded(touches, with: event)
    print("B")
    
    if let touch = touches.first {
        print("C")
        let touchLocation = touch.location(in: view)

        // 290 because the width of the view is 414, and the SettingsViewController width gets set to 0.7 * the view width in SlideInTransition.  0.7 * 414 is 289.8
        if touchLocation.x > 290 {
            dismiss(animated: true)
        }
    }
}

我做了 print 语句来查看它是否被调用,它不是。此视图控制器在自定义转换中呈现“菜单式”幻灯片。使用自定义转换显示的新 tablevc 的 UIView 的边界是否可能是问题所在?无论如何,我已经尝试实现 super.touchesEnded,或者添加所有覆盖触摸功能并在每次模拟时点击整个屏幕,但 touchesEnded 永远不会被调用。知道出了什么问题以及如何解决吗?谢谢

【问题讨论】:

  • 您正在使用 "'menu-esque' slide in custom transition" 来展示您的SettingsViewController ... UITableViewController?您是否在该表格视图上获得标准触摸事件?行选择、滚动、与单元格内容 UI 元素的交互等?
  • 嗯,它使用的是静态单元格,因为我本质上只是将它用作设置选项卡。因此,您可以点击其中一个单元格中的按钮,但不能滚动、行选择等。
  • 实际上这可能会产生误导,我可以点击一个只有标签的单元格并且单元格的边界线消失,我认为它已被选中。为什么呢?如果没有启用这些是问题吗?
  • 您是通过代码创建控制器吗?还是在 Storyboard 中设计它们?
  • tableviewController 在我的故事板中,但在我的代码中使用字符串标识符实例化,在点击按钮时

标签: ios swift xcode uitableview touches


【解决方案1】:

我猜你将touchesEnded() func 放在你的表格视图控制器中 ...这是行不通的。表格视图本身正在消耗触摸。

这可能对你有用...

将此UITableView 子类添加到您的项目中:

class TouchableTableView: UITableView {
    
    var callback: (() -> ())?
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("A")
        
        super.touchesEnded(touches, with: event)
        print("B")
        
        if let touch = touches.first {
            print("C")
            let touchLocation = touch.location(in: self)
            
            // not clear where you're trying to check for touch, so this may need to be changed
            
            // 290 because the width of the view is 414, and the SettingsViewController width gets set to 0.7 * the view width in SlideInTransition.  0.7 * 414 is 289.8
            if touchLocation.x > 290 {
                // this code is in a UITableView subclass,
                //  so we can't call dismiss() from here
                //dismiss(animated: true)

                // instead, we tell the Controller to take action
                callback?()
            }
        }
    }

}

然后,在 Storyboard 中,选择 Table View Controller 中的 Table View 并将其自定义类分配给 TouchableTableView

现在,touchesEnded() 将在自定义 TouchableTableView 类中调用(除非您点击单元格中的交互式 UI 元素,例如按钮)。

然后我们使用“回调”闭包告诉控制器触摸。因此,在您的 UITableViewController 中,将其添加到 viewDidLoad()

class ExampleTableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // we're in a UITableViewController,
        // so make sure the tableView is our TouchableTableView class
        // if it is, setup the closure
        if let tv = tableView as? TouchableTableView {
            tv.callback = {
                // here is where we would, for example, call dismiss()
                self.dismiss(animated: true, completion: nil)
            }
        }

    }

}   

【讨论】:

  • 这很好用,我必须修复一些实际的触摸位置特性,就像你在代码注释中建议的那样,但这是一个单独的问题。非常感谢您的帮助!
最近更新 更多