Make UIButton's state change to UIControlStateSelected on TouchUp - ios

I've noticed that if I do something like this
[self setTitle:#"Follow" forState:UIControlStateNormal];
[self setTitle:#"Following" forState:UIControlStateSelected];
The button will start off with "Follow". I press it, and when I release my finger (touch up event) it changes to "Following". That's great. However, when It's now selected and I press it, it immediately changes to "Follow" before I release my finger (on a touch down event). I want to change when I release my finger (touch up event). How do I do this? I am using UIControlStates for a UIButton's title, images, and title color.
Below is my event handler:
- (void)followButtonAction:(UIButton *)button
{
button.selected = !button.selected;
}
and I set it as so:
[self.followButton addTarget:self action:#selector(followButtonAction:) forControlEvents:UIControlEventTouchUpInside];

try to use this, This works for me as toggle button.
-(IBAction)buttonTapped:(UIButton *)sender{
if ([sender isSelected]) {
[sender setSelected:NO];
}else{
[sender setSelected:YES];
}
}
Hope this works for you too.

Turns out the solution is to add another control state specifically for the combination that is causing the problem (when selected, and then being pressed). Below is the solution:
[self setTitle:#"Follow" forState:UIControlStateNormal];
[self setTitle:#"Following" forState:UIControlStateSelected];
[self setTitle:#"Following" forState:UIControlStateSelected | UIControlStateHighlighted];

Related

How do i control images of two button with one button?

I have two buttons which is used to display two background image. When i press buttonOne image should appear on it while the image of buttonTwo should disappear and vice-versa.
-(IBAction)buttonOne:(UIButton *)sender{
[sender setBackgroundImage:myimage1 forState:UIControlStateNormal];
};
-(IBAction)buttonTwo:(UIButton *)sender{
[sender setBackgroundImage:myimage2 forState:UIControlStateNormal];
};
I can't put code this way
-(IBAction)buttonTwo:(UIButton *)sender{
[sender setBackgroundImage:myimage2 forState:UIControlStateNormal];
[buttonOne setBackgroundImage:nil forState:UIControlState Normal];
};
Obviously i can't put sender in place of buttonOne in place of sender.
putting buttonOne makes it unusable to use same code in every button, so i need a reusable code in place of buttonOne.
To add more, i have 25 buttons so which button's background i want to set nil depends on my code;
Try this:
-(IBAction)buttonOne:(UIButton *)sender{
[sender setBackgroundImage:myimage1 forState:UIControlStateNormal];
[btn_2 setHidden:NO];
[btn_1 setHidden:YES];
};
-(IBAction)buttonTwo:(UIButton *)sender{
[sender setBackgroundImage:myimage2 forState:UIControlStateNormal];
[btn_1 setHidden:NO];
[btn_2 setHidden:YES];
};
You can do that by giving different tag to both button and code as bellow
if(sender.tag == buttonOne.tag)
{
[buttonOne setBackgroundImage:myimage1 forState:UIControlStateNormal];
[buttonTwo setBackgroundImage:nil forState:UIControlStateNormal];
}
else if(sender.tag == buttonTwo.tag)
{
[buttonOne setBackgroundImage:nil forState:UIControlStateNormal];
[buttonTwo setBackgroundImage:myimage2 forState:UIControlStateNormal];
}

UIButton Response to Touch Down Event

I'm trying to mimic the button behavior of iPhone's Stopwatch app. So I added the following code to Touch Down event of the button.
if ([sender.titleLabel.text isEqualToString:#"Start"])
{
[sender setTitle:#"Stop" forState:UIControlStateNormal];
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
else
{
[sender setTitle:#"Start" forState:UIControlStateNormal];
[sender setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
But the problem is that the text and color don't change as soon as the button is pressed down, which I understand because of what I have as state, forState:UIControlStateNormal. Therefore, in the code above I replaced
forState:UIControlStateNormal
everywhere with
forState:UIControlStateHighlighted|UIControlStateNormal
But now the problem is that for as long as button is pressed down, it shows 'Stop' but as soon as I release the button it switches back to 'Start'. I don't understand this behavior. Any help will be appreciated.
Try to delete the UIControlStateNormal from here:
forState:UIControlStateHighlighted|UIControlStateNormal
and keep only
forState:UIControlStateHighlighted
EDIT:
Just another thing you can try is to create an integer_t variable (on your ViewDidLoad and set it to 2) and increase it everytime the button is pressed and check if the number is even or odd.
Code should be something like this:
if (intVariable % 2 == 0)
{
[sender setTitle:#"Stop" forState:UIControlStateHighlighted|UIControlStateNormal];
[sender setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted|UIControlStateNormal];
}
else
{
[sender setTitle:#"Start" forState:UIControlStateHighlighted|UIControlStateNormal];
[sender setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted|UIControlStateNormal];
}
intVariable++;

Dim UIButton text when tapped

I am using custom UIButtons, and what I want to do is make it so that when a user touches the button it dims, or turns a grayish color, like the regular button does.
I just want the text to temporarily change color temporally when the users lifts there figure it will be back to the regular color.
I have tried this code:
button.showsTouchWhenHighlighted = TRUE;
but that just makes a white circle around it which is not what I'm looking for.
Thanks for the help.
The easiest way I've come across is:
[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
Try changing alpha of title label on button click like this
- (IBAction)buttonClick:(id)sender {
UIButton *button = sender;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animations:^
{
[button.titleLabel setAlpha:0.5];
} completion:^(BOOL finished)
{
[button.titleLabel setAlpha:1];
}];
}
You can utilize the following UIControlEvents:
UIControlEventTouchDown
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
Assign target-action methods to them appropriately like so:
//when touch initiated
[myButton addTarget:self
action:#selector(buttonTouchStartAct:)
forControlEvents:UIControlEventTouchDown];
//on touch released outside button bounds
[myButton addTarget:self
action:#selector(buttonTouchEndAct:)
forControlEvents:UIControlEventTouchUpOutside];
//on touch released while still inside button bounds (most commonly used)
[myButton addTarget:self
action:#selector(buttonAct:)
forControlEvents:UIControlEventTouchUpInside];
-(void)buttonTouchStartAct:(UIButton *)sender
{
[sender.titleLabel setTextColor:[UIColor redColor]];
}
-(void)buttonTouchEndAct:(UIButton *)sender
{
[sender.titleLabel setTextColor:[UIColor greenColor]];
}
-(void)buttonAct:(UIButton *)sender
{
//touch ended in this case too
[self buttonTouchEndAct:sender];
//...
//your main button logic
//...
}
//when touch initiated
[myButton addTarget:self
action:#selector(buttonTouchStartAct:)
forControlEvents:UIControlEventTouchDown];
//on touch released outside button bounds
[myButton addTarget:self
action:#selector(buttonTouchEndAct:)
forControlEvents:UIControlEventTouchUpOutside];
//on touch released while still inside button bounds (most commonly used)
[myButton addTarget:self
action:#selector(buttonAct:)
forControlEvents:UIControlEventTouchUpInside];
and then inside the method change the color of the button

UI Button Images Sticking iOS 6

I have this method for a twitter button
-(IBAction)tweetThis:(id ) sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"TwitterButtonBlue.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"TwitterButtonBlack.png"] forState:UIControlStateSelected];
[sender setSelected:YES];}
}
in the simulator when i tap the button (which I made in IB as a touch up inside) the button clicks and changes to images but it doesn't go back to the original button image so for instance the blue.png changes to black.png but stays black and doesn't go back to blue. Any way I canf ix this
If I understand correctly, you're just trying to change the button image when the button is tapped. If so, you can simply just configure the button (in viewDidLoad or interface builder) like this:
[button setImage:[UIImage imageNamed:#"TwitterButtonBlue.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"TwitterButtonBlack.png"] forState:UIControlStateSelected];
The UIButton will handle the image switching for you.

UIButton - On touch change image

When I touch the button at that time I want to change image & when i release the touch button image is as it is.
I want to apply below code but it's not with my expectation.
please give me any suggestion.....
-(IBAction)actionEnter:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateSelected];
[sender setSelected:YES];
}
You can use UIControlStateHighlighted for this.
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateHighlighted];
You can also set this from interface builder by setting the image for highlighted state.
I think this should do it. Set the images after creating the button
[yourButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
and do this
- (IBAction)actionEnter:(id)sender{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
In Swift:
button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])
I think, you could set the image in the beginning for normal and selected state ..
Try with below when you create the UIButton object. [Use the images as per your requirement]
[myButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
#7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.
In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.
self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite-Selected"]
forState:UIControlStateSelected];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite"]
forState:UIControlStateNormal];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore-Selected"]
forState:UIControlStateSelected];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore"]
forState:UIControlStateNormal];
If you are just toggling a button on or off, you won’t need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.
- (void)favoriteIgnore:(UIButton *)buttonPressed {
// Toggle the tapped button
buttonPressed.selected = ( buttonPressed.selected) ? NO : YES;
id <ScoringToolbarDelegate> TB_delegate = _delegate;
// Turn off the other button and call the delegate
if ([buttonPressed.currentTitle isEqualToString:#"favorite"]) {
self.ignoreButton.selected = NO;
[TB_delegate favoriteButtonPressed];
} else {
self.favoriteButton.selected = NO;
[TB_delegate ignoreButtonPressed];
}
}
to change the image immediately use the backgroundImage property.

Resources