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.
The highlighted state is a transitional state that you can use to apply visible highlights to the cell while the user's finger is still touching the device. This state is set to YES only while the collection view is tracking touch events over the cell.
A control that executes your custom code in response to user interactions.
make your button Type - "Custom" and Uncheck - Highlighted Adjust image and you are done. Save this answer. Show activity on this post.
In storyboard choose the UIButton you want to change. In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With