How do I make a button stay pressed? - ios

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!

Put an image in button's attribute selected image in the XIB and when pressed
[(UIButton *)[self.view viewWithTag:buttonTag] setSelected:Yes];

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 .

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.

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];
}

Related

UIButton changes states when being pressed

I have an UIButton configured with two states, default and selected, for different text and background, the set up is done on Interface Builder. The weird thing is when the button is in selected state, and the button is pressed, it automatically changes to default state. After the button is released, it changes back to selected state. Is there way to disable roll-back-to-default behavior?
Forgot to mention that, the type is custom.
Update
tried the following code in viewDidLoad
[self.button setTitle:#"default" forState:UIControlStateNormal];
[self.button setTitle:#"selected" forState:UIControlStateSelected];
[self.button setTitle:#"highlighted" forState:UIControlStateHighlighted];
[self.button setSelected:YES];
The button shows default when pressed.
Selected state and Highlighted state are not mutually exclusive. When you press a selected UIButton, it gets into a selected AND highlighted state.
I don't think there's a way to set that in the IB, but you can do this in code:
[button setImage:[UIImage imageNamed:#"some_image"] forState:UIControlStateSelected | UIControlStateHighlighted];

UIButton titleColor upon selection and highlight

I'm trying to set a UIButton's titleColor to a certain color when it is selected and when it is highlighted, however, I need to set the button to be selected when the user touches down on the UIButton.
I've set it up like so:
[button setTitleColor:normalColor forState:UIControlStateNormal];
[button setTitleColor:superDuperSpecialColor forState:UIControlStateHighlighted];
[button setTitleColor:superDuperSpecialColor forState:UIControlStateSelected];
[button addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventTouchDown];
But when the button gets selected in the action: method using [senderButton setSelected:YES], it sets the titleColor to normalColor, rather than superDuperSpecialColor, which it should be, as it's both highlighted AND selected.
Commenting out the setSelected: call prevents the button from becoming and staying selected and commenting out the highlighted state color doesn't have any effect, it seems.
Will I have to add targets for UIControlEventTouchCancel, UIControlEventTouchUpInside and UIControlEventTouchUpOutside in order to call setSelected: after the highlight ends -or- change the titleColor for UIControlStateNormal to superDuperSpecialColor when the button gets a touch?
On a side note, I would have liked to set the titleColor like so:
[button setTitleColor:superDuperSpecialColor
forState:(UIControlStateHighlighted | UIControlStateSelected)];
But that doesn't seem to work. Why is that? Does Objective-C check for state equivalency?
I verified your results, and it seems like a bug in iOS. It fails on both the simulator and the device (iOS 6.1). It seems like if the selected and highlighted states are both YES, then the selected settings should override the highlighted settings. It's mostly implemented that way. The button's text value works like this, but the color seems to get it wrong (defaults to normal).
You might want to try it against iOS 7 if you have the latest XCode to see if they've fixed this, otherwise report it as a bug.
Since selected isn't a commonly used state for a UIButton, it probably wasn't properly tested in combination with other states.
As a workaround, in your action method, you could set the color for the normal state to superDuperSpecialColor and add another action for the touch up events to set the normal color back to normal. Since the state while the button is pressed should never actually be normal, this won't break anything if they do fix it in the future.
For both selected and highlighted state it's also necessary to set title, while for only highlighted state title is taken from normal state. So, don't forget to add extra line:
[button setTitleColor:UIColor.blackColor forState:UIControlStateSelected];
[button setTitleColor:[UIColor.blackColor colorWithAlphaComponent:0.5f]
forState:UIControlStateSelected | UIControlStateHighlighted];
[button setTitle:#"Title" forState:UIControlStateSelected];
[button setTitle:#"Title" forState:UIControlStateSelected | UIControlStateHighlighted];
Сode below works fine without explicit setting title for highlighted state:
[button setTitleColor:UIColor.greyColor forState:UIControlStateNormal];
[button setTitleColor:[UIColor.greyColor colorWithAlphaComponent:0.5f]
forState:UIControlStateHighlighted];
[button setTitle:#"Title" forState:UIControlStateNormal];
I think you need to check your UIButton type, in creation. I have code like this and works fine.
But setting state two states in one line doesn't work for me.
[button setTitleColor:superDuperSpecialColor
forState:(UIControlStateHighlighted | UIControlStateSelected)];

UIButton setSelected: NO doesnt show default background image

I have a custom UIButton with 2 background images:
in Default State: default.png
in Selected State: selected.png
set in the Xib File.
This is gets invoked on button touch down:
-(IBAction)numberSelected:(id)sender{
NSLog(#"Button pressed %#",[sender currentTitle]);
UIButton* button = (UIButton*)sender;
button.selected = !button.selected;
[button release];
}
The wrong behavior is this:
I press the button, the background image switches to selected.png (correct), I press it again, and no background image is shown. I press it another time, the app crashes.
I think you forgot to set : UIControlStateSelected adding the image.
[button setImage:[UIImage imageNamed:#"Selected.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
Edit:
Why do you make [button release]; Of course it will crash the second time.
No need to release the button there. Remove
[button release];
[button release];
This statement has to be removed!

Setting an UIImage for UIControlStateHighlighted

board[i] is an array of UIButtons that I have created programmmatically, and I can't change their image for UIControlStateHighlighted:
[board[i] setImage:[UIImage imageNamed:#"block"] forState:UIControlStateNormal];
[board[i] setImage:[UIImage imageNamed:#"blockPressed"] forState:UIControlStateHighlighted];
When I press the button with the mouse in the simulator the image doesn't change. I think this is a very noob question, but I don't what the code doesn't work.
when adding button programatically do this:
add target of each same.
provide tag all button from 0 to count.
set UserInteraction to true
setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted if u want button to be highlited
Now button is pressed same method is called for all button: For example
-(void)ButtonTouched:(id)sender
{
UIButton *btntouched = sender;
NSLog(#"%#", btntouched);
[btntouched setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted];// it can be forState:UIControlStateNormal also
}
I don't think you are triggering the highlighted state. This could be because they are not set to have interaction enabled. Or there is something else missing from the way you set up your buttons.
The other thing you can try is to add a selector to each of the buttons for when they are touched, and then change the image by referencing sender for the selector function.
Assuming you've made sure your image doesn't return nil, this code should work:
[myUIButton setImage:[UIImage imageNamed:#"myHighlightedButtonImage.png"] forState:UIControlStateHighlighted];
It should work find if you call that line in your viewDidLoad().

UIButton Background image Change programmatically

How can I change the background image of my UIButton in click event? And refresh it in a few second with the previous image? I mean change it's background image on click and reset it after the click.
[yourButton setImage:[UIImage imageNamed:#"yourNormalImage.png"] forState:UIControlStateNormal];
[yourButton setImage:[UIImage imageNamed:#"yourImageWhenClicking.png"] forState:UIControlStateHighlighted];
put your clickEvent image as highlighted image of your button..
The above answer is not working because it doesn't change the background but the side image of the UIButton, to change the background image use:
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourimage"] forState:UIControlStateNormal];

Resources