Can someone tell me how I can scale an UIButton
on touch? The button should scale up like 10%.
Thanks in开发者_如何学运维 advance!
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).
Swift 5
To make it feel more like native UIButton behaviour I prefer to use touchesBegun
and touchesEnded
methods in a subclass:
class BaseButton: UIButton {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
UIView.animate(withDuration: 0.3) {
self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
self.titleLabel?.alpha = 0.7
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
UIView.animate(withDuration: 0.3) {
self.transform = .identity
self.titleLabel?.alpha = 1
}
}
}
Usage
In your storyboard, just make your buttons inherit from BaseButton
.
Slightly modified code from that of @ibm123 which avoids sudden resize issue.
- (IBAction) buttonTapAction:(UIButton *) sender {
[self animatePressedDown:sender duration:0.6 zoom:1.5];
}
- (void)animatePressedDown:(UIButton *) sender duration:(double) t zoom:(double) zoomX {
[UIView animateWithDuration:t delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
sender.transform = CGAffineTransformMakeScale(zoomX,zoomX);
} completion:^(BOOL finished) {
[UIView animateWithDuration:t delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
sender.transform = CGAffineTransformMakeScale(1,1);
} completion:nil];
}];
}
This is what i use
-(IBAction)heartButtonTapped:(UIButton*)sender {
[sender setSelected:!sender.isSelected];
[UIView animateWithDuration:0.6 delay:0.0 options:UIViewAnimationOptionAutoreverse animations:^{
sender.transform = CGAffineTransformMakeScale(1.5,1.5);
} completion:^(BOOL finished) {
sender.transform = CGAffineTransformMakeScale(1,1);
}];
}
Swift:
button.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)
Swift 5:
@objc func onProfileClick(_ sender: CTCircularButton) {
UIView.animate(withDuration: 0.5, animations: {
sender.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}) { (isComplete) in
UIView.animate(withDuration: 0.5) {
sender.transform = .identity
}
print("Your action here")
}
}
精彩评论