Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton scale on touch?

Can someone tell me how I can scale an UIButton on touch? The button should scale up like 10%.

Thanks in advance!

like image 545
trnc Avatar asked Sep 08 '10 18:09

trnc


People also ask

How do I scale a button image in Swift?

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.

What is UIButton?

A control that executes your custom code in response to user interactions.

How do you highlight a button in Swift?

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.


2 Answers

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];
like image 150
Vladimir Avatar answered Sep 18 '22 17:09

Vladimir


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).

like image 27
Old McStopher Avatar answered Sep 18 '22 17:09

Old McStopher