在Swift中将UIView上的手势传输到UITableView-iOS [英] Transfer gestures on a UIView to a UITableView in Swift - iOS
问题描述
我在页面顶部有一个UIView
布局,在它的正下方有一个UITableView
.
I have a layout with a UIView
at the top of the page and, right below it, I have a UITableView
.
我想要做的是将UIView
上的手势交互转移到UITableView
,因此,当用户在UIView
上进行上下拖动时,UITableView
垂直滚动.
What I am wanting to do is to transfer the gesture interactions on the UIView
to the UITableView
, so when the user makes a drag up/down on the UIView
, the UITableView
scrolls vertically.
我尝试了以下代码
tableView.gestureRecognizers?.forEach { uiView.addGestureRecognizer($0) }
但是它以某种方式从UITableView
中删除了手势手势:/
but it removed the gestureRecognizers from the UITableView
somehow :/
糟糕:UIView
不能是UIScrollView
Obs.: the UIView
cannot be a Header
of the UIScrollView
推荐答案
那很棘手
不允许您的顶视图通过其后面的视图...
Your top view is not allowed to pass through view behind it...
可能的解决方案
- 通过所有接触来查看其背后的内容(实际上似乎不可能或很难)
- 告诉窗口以忽略顶视图上的触摸(简单易用)
第二个选项更好更容易.
Second option is better and easy.
所以您需要做的是创建UIView
和override
So What you need to do is create subclass of UIView
and override
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
,如果您在hitTest
动作
经过测试的有效示例
class PassThroughME : UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(point, with: event) == self ? nil : self
}
}
现在就是使用PassThroughME
了,方法是在情节提要中将类添加到视图中,或者以编程方式以任何方式添加视图
That's it now use PassThroughME
either by adding class to your view in storyboard or programmatically whatever way you have added your view
检查图像,我仍具有黑色的颜色,顶部还有0.7 alpha,我仍然可以滚动
Check image i have black color view with 0.7 alpha on top still i am able to scroll
希望对您有帮助
这篇关于在Swift中将UIView上的手势传输到UITableView-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!