Predict the output
A label with isUserInteractionEnabled = false sits inside a custom view that overrides touchesBegan. The user taps the label. Which view gets touchesBegan?
final class CardView: UIView {
let label = UILabel() // isUserInteractionEnabled is false by default
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("card touched")
}
}Hint 1 of 1
Which views does hit-testing skip?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B The CardView
Correct. The label is skipped, so the card is the deepest eligible view.
Why not the others?
- A The label. Hit-testing skips views with user interaction disabled.
- C Only the window. The card is eligible and deeper than the window.
- D Nobody. The card has interaction enabled and handles the touch.