What control events start and end the UIButton backlight state

I am creating a piano look with UIButton as piano keys. What UIControlEvents should I listen to to receive callbacks when the button receives and loses the selected state?

I tried to subclass UIButton and add the observer property for highlighted , and it worked fine. However, sometimes I need to set the selected state manually from the code, and this really messed it up, because there is no way to determine if the event was triggered by the user or the application.

+5
source share
2 answers

To simulate the behavior of piano keys, I used the following UIControlEvents :

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

gets allocated state: UIControlEventTouchDown

lose highlighted state: UIControlEventTouchDragOutside

0
source

Source: https://habr.com/ru/post/1240436/


All Articles