Dim UIButton text when tapped - ios

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

Related

change UIButton title without fade effect?

When I change the title of a UIButton it has this effect where the old title fades out and the new one fades in. Can I change the title without the animation?
[self.a setTitle:#"a" forState:UIControlStateNormal];
For system UIButton, using:
[self.a layoutIfNeeded];
For custom UIButton, using:
[UIView setAnimationsEnabled:NO];
[self.a setTitle:#"a" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
There is two way to do this
1.System Button without animation
- (IBAction)actionChangeButtonTitle:(id)sender
{
[UIView setAnimationsEnabled:NO];
[buttonTitleChange setTitle:#"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
[buttonTitleChange layoutIfNeeded]; //For System Buttons
}
2.Custom Button Without animation
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self
action:#selector(actionChangeButtonTitle:)forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Change Title" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:btn];
- (IBAction)actionChangeButtonTitle:(id)sender
{
[btn setTitle:#"title" forState:UIControlStateNormal];
}
Thank You-:)

How to change button's Image on clicking it in ios

Confession: I have been through every possible link I could find, but unfortunately none work for me.
I am creating buttons dynamically. When a button is selected, it should change the color of the selected button and the rest should remain the default color. This way, the user can identify which button is selected.
Suppose there are 3 buttons: all blue in color, and when I select the first one, it should change to white and other two should remain blue. When I select the second, the first one should go back to blue and the second should now be white.
btnCounts data I am fetching from server and would vary.
-(void)createButtons{
for (int i=0; i<btnCounts; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTag:i];
[btn setFrame:CGRectMake(xVal, 0, width/btnCounts, 40)];
// [btn setImage:[UIImage imageNamed:#"btn_image.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"white.png"] forState:UIControlStateSelected];
[btn setTitle:name forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrV addSubview:btn];
xVal += self.view.frame.size.width/btnCounts+1;
}
}
Now, in btnClicked: method I am passing the button as a parameter, so that I can use selected button.
-(void)btnClicked:(UIButton *)button
{
int tag = (int)button.tag;
//...
}
Please help me to find what I am missing here.
Thanks.
Create an instance UIButton variable to house the selected button so that you can deselect it when the next button is tapped. If you only pass the currently-tapped button to your btnClicked: method, you won't know which button to deselect (if any).
#implementation ClassName {
UIButton *previousButton;
}
...
- (void)btnClicked:(UIButton *)button
{
if(previousButton)
[previousButton setBackgroundColor:[UIColor blueColor]];
previousButton = button;
[button setBackgroundColor:[UIColor whiteColor]];
// do whatever else you need to do here
}
For that you need to store selected button in temporary object.
Create button in interface(.h) file:
UIButton *selectedButton;
Than add following line in -(void)createButtons method before for loop.
selectedButton = nil;
for() {...}
Here selectedButton is set to nil because initially none of the buttons are selected.
And finally replace your -(void)btnClicked:(UIButton *)button method with following method.
-(void)btnClicked:(UIButton *)button {
if(selectedButton) {
selectedButton.selected = NO;
}
button.selected = YES;
selectedButton = button;
}
If you want to make any button initially selected than assign that button to selectedButton like in btnClicked method.

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++;

Delay of UIButton's highlighted state on iOS 7

I created new project in Xcode - Single View app.
App have only two buttons.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setBackgroundColor:[UIColor greenColor]];
[button1 setFrame:CGRectMake(0, self.view.frame.size.height-40-100, self.view.frame.size.width, 40)];
[button1 setTitle:NSLocalizedString(#"button 1", nil) forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setBackgroundColor:[UIColor greenColor]];
[button2 setFrame:CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width, 40)];
[button2 setTitle:NSLocalizedString(#"button 2", nil) forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button2];
When I run this app on iPhone with iOS 7 second button have delay of highlighted state when I press this button. On iPhone with iOS 6 second button works perfect.
Why button on iOS 7 have delay of highlighted?
My problem was I had a UIButton as a subview of a paging UIScrollView, so I wanted the user to be able to do a right swipe over the area where the button was without it pressing the button. In iOS6 if you do this over a rounded rect button, it works fine, but in iOS7 it also works but the button won't trigger it's highlight. So to fix this, I implemented my own UIButton using the longPressGestureRecognizer:
- (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer
{
if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
if (CGRectContainsPoint(self.bounds, touchedPoint))
{
[self addHighlights];
}
else
{
[self removeHighlights];
}
}
else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
if (self.highlightView.superview)
{
[self removeHighlights];
}
CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
if (CGRectContainsPoint(self.bounds, touchedPoint))
{
if ([self.delegate respondsToSelector:#selector(buttonViewDidTouchUpInside:)])
{
[self.delegate buttonViewDidTouchUpInside:self];
}
}
}
}
Then when you initialize the longPressGestureRecognizer and you do:
self.longPressGestureRecognizer.minimumPressDuration = .05;
This will allow you to do a swipe over the button without it triggering it, and will also enable you to press the button and have its highlight trigger. Hope this helps.
Try to overload this method in your scroll view subclass:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
// fix conflicts with scrolling and button highlighting delay:
if ([view isKindOfClass:[UIButton class]])
return YES;
else
return [super touchesShouldCancelInContentView:view];
}
I'm not sure if the OP just wants visual feedback, but if so, setting the showsTouchWhenHighlighted property to YES/true in code or checking the Shows Touch On Highlight option in IB will accomplish that.

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