I have two images for a button, one for normal, and one for selected. And i want it to act the same way a rounded rect button acts as when you touch down, the buttons image changes, and if you drag outside, the image changes back to normal. Heres what i tried.
First, I made a custom view controller, and then i added methods in the implementation to it which is what you see down here. And i hooked these methods up to my 2 buttons, my play ‘button’ and my ‘settings’ button.
- (IBAction)playButtonSelected:(UIButton *)sender { //this is for event touchDown
[sender setImage:[UIImage imageNamed:@"PlayButtonSelected.png"] forState:UIControlStateNormal];
}
- (IBAction)playButtonNotSelected:(id)sender { //this is for event touchUpOutside
[sender setImage:[UIImage imageNamed:@"PlayButton.png"] forState:UIControlStateNormal];
}
- (IBAction)settingsButtonSelected:(UIButton *)sender { //this is for event TouchDown
[sender setImage:[UIImage imageNamed:@"SettingsButtonSelected.png"] forState:UIControlStateNormal];
}
- (IBAction)settingsButtonNotSelected:(id)sender { //this is for event touchUpOutside
[sender setImage:[UIImage imageNamed:@"SettingsButtons.png"] forState:UIControlStateNormal];
}
Well, I did this, and it worked pretty good, but there are 2 problems.
1st: If I have like 20 buttons I would have to have 40 methods. This isn’t really a problem, I just think its something to think about as a potential issue.
2nd: When I drag my button outside, I would like for it to switch to the normal image before the user actually releases his finger. As of right now the image only defaults when the finger is released, and not when the finger is moved at a certain distance so that the touchUpInside command will not run. I want this because its basically the standard. And it would also help the user to understand that he can take back his impulsive behavior of clicking buttons if he so desires.
Cool, any help would be a lot of help!
You only need to set the selected and normal images once (when you create the button for instance)
the “state” argument lets you set different images for different states. You only need to write this code once :
Edit: you can also do this from Interface Builder from the attributes inspector