Transfer gestures on a UIView to a UITableView in

2020-04-30 03:06发布

I have a layout with a UIView at the top of the page and, right below it, I have a 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.

I tried the following code

tableView.gestureRecognizers?.forEach { uiView.addGestureRecognizer($0) }

but it removed the gestureRecognizers from the UITableView somehow :/


Obs.: the UIView cannot be a Header of the UIScrollView

1条回答
我只想做你的唯一
2楼-- · 2020-04-30 03:43

That's Tricky

What is problem ?

Your top view is not allowed to pass through view behind it...

What would be possible solutions

  1. pass all touches to view behind it (Seems to not possible or very tough practically )
  2. Tell window to ignore touches on top view (Easy one)

Second option is better and easy.

So What you need to do is create subclass of UIView and override

 override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?  

and return nil if you found same view on hitTest action

Here Tested and working example

class PassThroughME : UIView {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        return super.hitTest(point, with: event) == self ? nil : self
    }
}

That's it now use PassThroughME either by adding class to your view in storyboard or programmatically whatever way you have added your view

Check image i have black color view with 0.7 alpha on top still i am able to scroll

enter image description here

Hope it is helpful

查看更多
登录 后发表回答