Skip to content

Instantly share code, notes, and snippets.

@wotjd
Created November 21, 2019 03:30
Show Gist options
  • Save wotjd/446c16ad842d630a2f8d3447442a83a6 to your computer and use it in GitHub Desktop.
Save wotjd/446c16ad842d630a2f8d3447442a83a6 to your computer and use it in GitHub Desktop.
A Button Gets Touch Event of Multiple Views (though not subview!)
import UIKit
class CustomButton {
var containingTouchViews: [UIView] = []
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
self.containingTouchViews
.map { view in
view.frame.contains(self.convert(point, to: view))
// below codes are optional
&& view.isUserInteractionEnabled
&& !view.isHidden
&& view.alpha > 0
}
.filter { $0 }
.isNotEmpty
|| super.point(inside: point, with: event)
}
}
// Usage
class ViewController: UIViewController {
let button = CustomButton()
let otherView = UIView()
...
override func viewDidLoad() {
super.viewDidLoad()
self.button.containingTouchViews.append(self.otherView)
// then, otherView's touch event passes to the button
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment