import UIKit class TestView: UIView { var name: String = "" override init(frame: CGRect) { super.init(frame: frame) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { print("⭐️ \(name) - hitTest enter") let res = super.hitTest(point, with: event) print("💗 \(name) - hitTest result: \(res)") return res } override func touchesBegan(_ touches: Set, with event: UIEvent?) { print("\(name) - touchesBegan") super.touchesBegan(touches, with: event) } override func touchesEnded(_ touches: Set, with event: UIEvent?) { print("\(name) - touchesEnded") super.touchesEnded(touches, with: event) } override func touchesCancelled(_ touches: Set, with event: UIEvent?) { print("\(name) - touchesCancelled") super.touchesCancelled(touches, with: event) } } class ViewController: UIViewController { private lazy var button = { let b = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 50, height: 50))) b.setTitle("btn", for: .normal) b.setTitleColor(.black, for: .normal) return b }() private lazy var orangeView = { let v = TestView(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: 200, height: 500))) v.backgroundColor = .orange v.name = "orange" let ges = UITapGestureRecognizer(target: self, action: #selector(tapped(ges:))) v.addGestureRecognizer(ges) return v }() private lazy var blueView = { let v = TestView(frame: CGRect(origin: CGPoint(x: 20, y: 20), size: CGSize(width: 150, height: 150))) v.backgroundColor = .blue v.name = "blue" return v }() private lazy var greenView = { let v = TestView(frame: CGRect(origin: CGPoint(x: 20, y: 180), size: CGSize(width: 100, height: 100))) v.backgroundColor = .green v.name = "green" return v }() private lazy var blackView = { let v = TestView(frame: CGRect(origin: CGPoint(x: 20, y: 300), size: CGSize(width: 70, height: 70))) v.backgroundColor = .black v.name = "black" return v }() private lazy var redView = { let v = TestView(frame: CGRect(origin: CGPoint(x: 20, y: 20), size: CGSize(width: 100, height: 100))) v.backgroundColor = .red v.name = "red" return v }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white button.addTarget(self, action: #selector(buttonTap(sender:)), for: .touchUpInside) view.addSubview(orangeView) orangeView.addSubview(blueView) orangeView.addSubview(greenView) orangeView.addSubview(blackView) blueView.addSubview(redView) redView.addSubview(button) } @objc func tapped(ges: UITapGestureRecognizer) { print("UITapGestureRecognizer - tap") } @objc func buttonTap(sender: UIButton) { print("button - tap") } override func touchesBegan(_ touches: Set, with event: UIEvent?) { print("ViewController - touchesBegan") super.touchesBegan(touches, with: event) } override func touchesEnded(_ touches: Set, with event: UIEvent?) { print("ViewController - touchesEnded") super.touchesEnded(touches, with: event) } override func touchesCancelled(_ touches: Set, with event: UIEvent?) { print("ViewController - touchesCancelled") super.touchesCancelled(touches, with: event) } }