Kirsteins
Kirsteins

Reputation: 27345

What control events start and end highlight state of UIButton

I am creating piano-like view with UIButton as piano keys. What UIControlEvents should I listen for to get callbacks when button gets and loses highlighted state?

I tried to make subclass of UIButton and add property observer for highlighted and it was working fine. However sometimes I need to set highlighted state manually from code and that really messes it up as there is no way to tell whether event was user or app initiated.

Upvotes: 9

Views: 4426

Answers (2)

Kirsteins
Kirsteins

Reputation: 27345

To mimic piano key behavior I used the following UIControlEvents:

self.addTarget(self, action: "pressed", forControlEvents: [.touchDown])
self.addTarget(self, action: "released", forControlEvents: [.touchDragExit, .touchUpInside, .touchUpOutside, .touchCancel])

Upvotes: 20

jiangjiefs
jiangjiefs

Reputation: 149

gets highlighted state:UIControlEventTouchDown

lose highlighted state:UIControlEventTouchDragOutside

Upvotes: 2

Related Questions