简体   繁体   中英

UIButton stay selected after Touch

I'd like to have a UIButton stay in the Selected state after touching it. I've added the code to set the selected state during the TouchUpInside event; however, after a quick tap there's a slight "blink" between states. I have a few strategies for doing this using an UImageView and adding a UITapGestureRecognize , but there has to be an easier way.

The reason for the flash is that Touch Up Inside is too late; the user's finger is already up, by definition. Perhaps you should consider implementing an action event for Touch Down Inside instead (or in addition).

Another possibility might be to use a UISegmentedControl with one segment and momentary set to NO.

This is just off the top of my head

A UIBUtton has four states - Normal, Highlighted, Selected, Disabled I think the 'blink' you are noticing is due to the highlighted state. The button probably goes through the highlighted state when tapped making it 'blink'

Try setting the highlighted state properties the same as the selected state.

try this,

[button sendActionsForControlEvents:UIControlEventTouchUpInside];
[button setSelected:YES];

Create a UIView looks similar to the button. Just add the button as subview of a UIView and also add button's title(UILabel) as subview of the same UIView. On pressing the button, change the properties of the UIView and UILabel as to make it look like pressed button so that the view can stay as pressed state until you change those properties again.

Hope this helps.

Thanks for the great answers. Rather than dealing with UIButton, I ended up using a UIImageView and set the highlighted state in an animation block based on a tap gesture. It made it much easier to accomplish a pressed state rather than trying to account for UIButton gesture handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM