Vla*_*mir 21
呼叫
button.transform = CGAffineTransformMakeScale(1.1,1.1);
Run Code Online (Sandbox Code Playgroud)
按下按钮处理程序.
或者如果你想用动画缩放:
[UIView beginAnimations:@"ScaleButton" context:NULL];
[UIView setAnimationDuration: 0.5f];
button.transform = CGAffineTransformMakeScale(1.1,1.1);
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
Old*_*her 17
要完成答案,可以将按钮缩放(和重置)放入如下方法中:
// 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
}
Run Code Online (Sandbox Code Playgroud)
并与按钮的事件连接如下:
[btn addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpOutside];
Run Code Online (Sandbox Code Playgroud)
注意1:将CGAffineTransformMakeScale值设置为1.0不会使它们保持在更改的值(即,它不会将1.1乘以1.0),而是将其设置回对象的原始比例.
注意2:不要忘记:
选择器中的冒号,因为它允许将发送者作为参数传递给接收方法.在这种情况下,我们的方法接收UIButton,并在接口(.h文件)中声明为UIButton.
为了使其感觉更像本机 UIButton 行为,我更喜欢在子类中使用touchesBegun
和方法:touchesEnded
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的故事板中,只需让你的按钮继承自BaseButton
.