ios - 如何让 scrollview 将触摸事件传递给 superview?

标签 ios swift uitableview uiscrollview uistackview

我正在尝试创建一个 UITableViewCell它有一个水平滚动的 UIScrollView .如何确保当用户触摸 UIScrollView , UITableViewCell被选中。

UITableViewCell有一个 UIScrollView作为 subview 以及其他一些 subview 。当用户点击非 UIScrollView 时 subview ,单元格按预期被选中。 但是,当用户点击 UIScrollView , 没有任何反应。

UIScrollView有一些 subview 。用户可以按预期水平滚动 ScrollView 。

有什么方法可以让用户在浏览 UIScrollView 时, UIScrollView处理触摸事件,但是当用户点击 UIScrollView 时, 它将事件传递给 super View ?

编辑 - 我尝试覆盖 touchesEnded(_:with:) 如下-

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    superview?.touchesEnded(touches, with: event)
}

没用

其次,我尝试添加一个 UITapGestureRecognizer到 ScrollView 并检查是否检测到水龙头。 它确实检测到水龙头。但是,为了选择 UITableViewCell ,我需要得到相应的 UITableViewCell对于 ScrollView ,找到它的 indexPath 然后使用 UITableView选择该 indexPath。

我希望是否有更简单的方法来执行我想要实现的目标。

最佳答案

我通过向 ScrollView 添加 UITapGestureRecognizer 解决了这个问题。

在用户点击 ScrollView 时调用的函数中,我以编程方式选择如下行 -

guard let indexPath = tableView.indexPath(for: cell) else { return }
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
tableView(tableView, didSelectRowAt: indexPath)

关于ios - 如何让 scrollview 将触摸事件传递给 superview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53916006/

相关文章:

ios - 快速计算嵌套数组的元素数

swift - iPhone X、iPhone XS 等相机变焦问题

ios - UITableView reloadData 花费太多时间

ios - 缩进自定义表格 View 单元格

ios - Apple App如何获得YouTube授权

iOS Objective-C/Swift 下载时读取文件

ios - UISearchBar subview 奇怪的行为

ios - 当应用程序进入后台时无法设置亮度,任何人都可以对此有任何想法吗?

ios - 用户通知中心(_ :didReceive:withCompletionHandler:) not be called when a notification action is selected

ios - 如何使 UITableviewCell(自定义)高度取决于内容?