How do I make a button stay pressed?

随声附和 提交于 2019-12-25 11:32:11

问题


Okay so I have an app I'm building that is almost complete. I'm just having one problem, when I press my button it makes a sound and the button's image changes. However the image only changes when it is being touched or "highlighted" and I would like the buttons image to remain changed through the duration of the sound effect then after the sound effect I would like it to revert back to the original image. Is there anyway I can set up a UIButton "set time" or something for the "highlighted" option?

I feel so embarrassed sometimes, because I seem to get tripped up by these most trivial things, when I handle the core coding exceptionally well and near finish a full app in a days time, but this is my first app and I'm still a newbie to XCode. I really appreciate this community's help any answer that pushes me forward is always appreciated!

I further apologize for my questions formatting I typed this on my iPhone I hope it's not too awkward or lacking in detail. If anyone needs more detail just ask!


回答1:


1.You dont want any reflection of image at highlighted and selected state. so you just put same image for all state like

[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];
[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateHighlighted];
[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateSelected];

2.Now if you want image change at sound stop time than make

[btnClear setBackgroundImage:[UIImage imageNamed:@"red_button.png"]
                            forState:UIControlStateNormal];
[btnClear setBackgroundImage:[UIImage imageNamed:@"red_button.png"]
                            forState:UIControlStateHighlighted];
[btnClear setBackgroundImage:[UIImage imageNamed:@"red_button.png"]
                            forState:UIControlStateSelected];

And again on button click you just toggle image with first one.




回答2:


Put an image in button's attribute selected image in the XIB and when pressed

[(UIButton *)[self.view viewWithTag:buttonTag] setSelected:Yes];



回答3:


It may be helpful

[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateHighlighted];

If this not succeed then use one of the state from the below

   UIControlStateNormal               ,
   UIControlStateHighlighted          ,
   UIControlStateDisabled             ,
   UIControlStateSelected             ,
   UIControlStateApplication          ,
   UIControlStateReserved             . 



回答4:


you can toggle background image on sound effect by using some delegate methods of the sound player. When you finish playing sound, change the back image.

e.g.

-(void) musicDidFinishPlaying
{
[btn setBackgroundImage:[UIImage imageNamed:@"normalImage.png"]
                            forState:UIControlStateNormal];

}

-(IBAction) buttonClicked:(id)sender
{
[btn setBackgroundImage:[UIImage imageNamed:@"soundImage.png"]
                            forState:UIControlStateNormal];
}


来源:https://stackoverflow.com/questions/18436866/how-do-i-make-a-button-stay-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!