Created
May 4, 2018 22:29
-
-
Save robtimp/f3aed16659282e9d9433493b250536f0 to your computer and use it in GitHub Desktop.
Reimplement UIView.hitTest in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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