Reputation: 2334
The isHighlighted
property on UIButton
is mostly analogous to whether the button is pressed or not.
The Apple docs are vague about it:
Controls automatically set and clear this state in response to appropriate touch events. https://developer.apple.com/documentation/uikit/uicontrol/1618231-ishighlighted
Which method is the system calling to set this?
It appears to be touchesBegan
, because you can override this method to prevent it.
But, surprisingly, if you manually call touchesBegan
it doesn't get set (for example, from a superview-- see code below).
So it seems it's not simple as touchesBegan
. I've tried overriding every method I could...pressesBegan
, pressesChanged
, pressesCancelled
, pressesEnded
, touchesCancelled
, beginTracking
, continueTracking
, hitTest
, etc.
Only touchesBegan
had any effect.
Here's how I tried calling touchesBegan from a superview:
class MyView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else { return }
for view in self.subviews {
if let button = view as? UIButton {
if button.frame.contains(point) {
button.touchesBegan(touches, with: event)
print("\(button.isHighlighted)") //prints false
}
}
}
}
}
Upvotes: 2
Views: 342