Skip to content

Instantly share code, notes, and snippets.

@hlung
Created January 5, 2016 06:51
Show Gist options
  • Save hlung/6b76023b2176f95a7cc6 to your computer and use it in GitHub Desktop.
Save hlung/6b76023b2176f95a7cc6 to your computer and use it in GitHub Desktop.
Make subviews touchable even if it is outside its superview bounds.
// Make subviews touchable even if it is outside its superview bounds.
// ** Add more views here **
lazy var touchableViews: [UIView] = {
return [self.voteUpButton, self.voteDownButton, self.authorDetailButton]
}()
// Boilerplate
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
for view in touchableViews {
if let v = view.hitTest(view.convertPoint(point, fromView: self), withEvent: event) {
return v
}
}
return super.hitTest(point, withEvent: event)
}
// Boilerplate
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
if super.pointInside(point, withEvent: event) {
return true
}
for view in touchableViews {
return !view.hidden && view.pointInside(view.convertPoint(point, fromView: self), withEvent: event)
}
return false
}
@turk-jk
Copy link

turk-jk commented Jun 27, 2020

how do use it ?

@hlung
Copy link
Author

hlung commented Jun 27, 2020

@turk-jk Add these code to the view that holds the subviews you want to be touchable.

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