Skip to content

Instantly share code, notes, and snippets.

@diegoperini
Last active March 4, 2019 16:02
Show Gist options
  • Save diegoperini/8dfa345fca7c733c411dbec6e93729b0 to your computer and use it in GitHub Desktop.
Save diegoperini/8dfa345fca7c733c411dbec6e93729b0 to your computer and use it in GitHub Desktop.
A parent view to redirect touch events to its first child, can be used to extend scrollable area of a small UIScrollView
// Make sure you disable user interractions on the first child of this view
class TouchProxyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let v = super.hitTest(point, withEvent: event)
if v == self {
if let c = v?.subviews.first {
return c
}
}
return v
}
}
@mihai-salari
Copy link

Swift 4.2

  override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    let v = super.hitTest(point, with: event)
    
    if v == self {
      if let c = v?.subviews.first {
        return c
      }
    }
    
    return v

  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment