Can someone tell me how I can scale an UIButton
on touch? The button should scale up like 10%.
Thanks in advance!
Using storyboard, select the button, then in the size inspect click the dropdown on size just above Content inset. There is a list of sizes to select from, and this will adjust your image size(if you use system image). The default size is already set when you added the button on the storyboard to the View Controller.
A control that executes your custom code in response to user interactions.
In storyboard choose the UIButton you want to change. In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button.
Call
button.transform = CGAffineTransformMakeScale(1.1,1.1);
In button pressed handler.
Or if you want to scale with animation:
[UIView beginAnimations:@"ScaleButton" context:NULL];
[UIView setAnimationDuration: 0.5f];
button.transform = CGAffineTransformMakeScale(1.1,1.1);
[UIView commitAnimations];
To complete the answer, the button scaling (and resetting) can be placed into methods like so:
// Scale up on button press
- (void) buttonPress:(UIButton*)button {
button.transform = CGAffineTransformMakeScale(1.1, 1.1);
// Do something else
}
// Scale down on button release
- (void) buttonRelease:(UIButton*)button {
button.transform = CGAffineTransformMakeScale(1.0, 1.0);
// Do something else
}
And connected with the button's events like so:
[btn addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpOutside];
NOTE1: Setting the CGAffineTransformMakeScale values to 1.0 does not keep them at their altered values (i.e., it does not multiply 1.1 by 1.0), but rather sets it back to the object's original scale.
NOTE2: Don't forget the colon :
in the selector, as it allows the passing of the sender as a parameter to the receiving method. In this case, our methods receive a UIButton and would be declared as such in the interface (.h file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With