Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 859
Kirsteins Avatar asked Jan 12 '16 13:01

Kirsteins


People also ask

What is the highlighted state?

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.

What is a UIButton?

A control that executes your custom code in response to user interactions.

How do I turn off highlighting in UIButton?

make your button Type - "Custom" and Uncheck - Highlighted Adjust image and you are done. Save this answer. Show activity on this post.

How do you highlight a button in Swift?

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.


1 Answers

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])
like image 94
Kirsteins Avatar answered Sep 17 '22 17:09

Kirsteins