有人可以告诉我如何在触摸时缩放UIButton
吗?该按钮应该按比例放大10%。
提前致谢!
答案 0 :(得分:20)
呼叫
button.transform = CGAffineTransformMakeScale(1.1,1.1);
按下按钮处理程序。
或者如果你想用动画进行缩放:
[UIView beginAnimations:@"ScaleButton" context:NULL];
[UIView setAnimationDuration: 0.5f];
button.transform = CGAffineTransformMakeScale(1.1,1.1);
[UIView commitAnimations];
答案 1 :(得分: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
}
与按钮的事件相关联如下:
[btn addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpOutside];
注1:将CGAffineTransformMakeScale值设置为1.0并不会使它们保持更改的值(即,它不会将1.1乘以1.0),而是将其设置回对象的原始比例。
注2:不要忘记选择器中的冒号:
,因为它允许将发件人作为参数传递给接收方法。在这种情况下,我们的方法接收一个UIButton,并在接口(.h文件)中声明为。
答案 2 :(得分:2)
这是我使用的
-(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);
}];
}
答案 3 :(得分:2)
稍微修改了@ ibm123的代码,避免了突然调整大小的问题。
- (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];
}];
}
答案 4 :(得分:1)
夫特:
button.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)
答案 5 :(得分:0)
快捷键5:
::v-deep .user {
...
}
答案 6 :(得分:0)
雨燕5
要使其更像本机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
}
}
}