Skip to content

Instantly share code, notes, and snippets.

@robtimp
Created May 4, 2018 22:29
Show Gist options
  • Save robtimp/f3aed16659282e9d9433493b250536f0 to your computer and use it in GitHub Desktop.
Save robtimp/f3aed16659282e9d9433493b250536f0 to your computer and use it in GitHub Desktop.
Reimplement UIView.hitTest in Swift
extension UIView {
func reimplementedHitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard isUserInteractionEnabled && !isHidden && alpha >= 0.01 else {
return nil
}
if point(inside: point, with: event) {
for subview in subviews {
let convertedPoint = subview.convert(point, from: self)
if let view = subview.reimplementedHitTest(convertedPoint, with: event) {
return view
}
}
return self
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment