This button is a series of images that animate a glowing effect, when the button is pressed I'd like the animation to stop:
[self.buttonHintdisabled setBackgroundImage: [UIImage animatedImageNamed:#"c" duration:3.0] forState: UIControlStateNormal];
How is this done?
If there is another way to do this in code that could disable it when the button is pressed, please let me know?
I haven't tried this on a UIButton, but I will give you an idea how I would do it:
Often times we set a target with our UIButton:
[self.buttonHintdisabled addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Within buttonPressed:, we would carry out what we would like to do when the button is pressed.
So, with that in mind, in addition to what you would like to do within buttonPressed, you could try:
[self.buttonHintdisabled setBackgroundImage:[UIImage imageNamed:#"c0.png"] forState:UIControlStateSelected];
Once the action within that method is done, set it back using what you have above:
[self.buttonHintdisabled setBackgroundImage:[UIImage animatedImageNamed:#"c" duration:3.0] forState: UIControlStateNormal];
if you want to use selected button state (UIControlStateSelected)
add this line
[self.buttonHintdisabled setBackgroundImage: [UIImage imageNamed:#"selectedImage"] forState: UIControlStateSelected];
and then in its action
- (void)buttonClicked:(id)sender
{
[self.buttonHintdisabled setSelected:YES];
}
or if you can just replace background image in - (void)buttonClicked:(id)sender
Related
Hi I'm trying to change the button image by clicking the another button for example i have two buttons like button1 and button2 already this two button has images but now I'm trying to change the second button image by clicking the first button in second button the new image should replace the old image please tell me how to do it.
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}
**[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"]** forState:UIControlStateNormal];
This above code is used to change the button image but i want know how to change the second button image by clicking image.
Thanks
You might have the IBAction property of a second button, which you can access in your implementation (.m) file with self (e.g. self.secondButtonPropertyName), now you can write the code like below:
-(IBAction)bt1:(id)sender
{
[self.btn2 setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
first you set your both button tag..like btn1 tag =1 and btn2 tag =2
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)[self.view viewWithTag:2];
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}
suppose you have two buttons btn1 and btn2
Now after them allocatng memory,
you can use below Button selector,
btn1 selector,
-(IBAction)btn1selector:(id)sender
{
[btn2 setbackgroundimage: [uiimage imagenamed:#"your image"]];
}
you can do same for the other button selector.
I am trying to update the title from u -> d on a button when it is selected. The setting to 'u' works fine but afer I click the button, it doens't update. I am putting it into the action but not sure if this is correct place.
in viewDidLoad
[self.expandButton setTitle:#"u" forState: UIControlStateNormal];
where do I put this? - not working correctly in the UIButton's action:
[self.expandButton setTitle:#"d" forState: UIControlStateSelected];
thx for any help
edit 1
Is set as a property:
#property (weak, nonatomic) IBOutlet UIButton *expandButton;
...
here's the action and how I am trying to set the selected state:
- (IBAction)expand:(id)sender {
[self.expandButton setTitle:#"d" forState: UIControlStateSelected];
You could create a function to set button title and call it on touch down:
[button addTarget:self action:#selector(buttonTouchDown:)forControlEvents:UIControlEventTouchDown];
...
EDITED
what I got is, when the button tapped, the state change to selected, and when tapped again state back to normal. you can use these trick :
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[button setTitle:#"unselected" forState:UIControlStateNormal];
[button setTitle:#"selected" forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonTap:(UIButton *)sender {
sender.selected = !sender.selected;
}
You could make a "blank" button and use a UILabel on top of it. I'm not sure of why you aren't getting updates but in my experience I usually deal with this situation in such a way.
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!
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().
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];