How do I make a button stay pressed?

2019-08-28 13:28发布

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!

4条回答
迷人小祖宗
2楼-- · 2019-08-28 13:46

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];
}
查看更多
Emotional °昔
3楼-- · 2019-08-28 13:56

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             . 
查看更多
小情绪 Triste *
4楼-- · 2019-08-28 14:01

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.

查看更多
Bombasti
5楼-- · 2019-08-28 14:03

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

[(UIButton *)[self.view viewWithTag:buttonTag] setSelected:Yes];
查看更多
登录 后发表回答