2010-09-08 111 views

回答

19

呼叫

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

好凉,我怎么能结合这与触摸事件? – Tronic 2010-09-08 19:48:41

+0

对不起,不明白你的意思究竟是什么意思...... – Vladimir 2010-09-08 20:01:26

+0

我的意思是,当按下按钮时,我怎样才能将这段代码与事件结合起来。 – Tronic 2010-09-08 21:55:24

15

要完成答案,按钮缩放(和复位)可以被放置到方法,像这样:

// 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文件)中声明。

+4

别忘了'UIControlEventTouchCancel'和'UIControlEventTouchDragExit' – Tim 2013-01-09 08:07:29

+0

super.Thank you so much ..它完美 – 2014-03-18 07:50:06

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); 
    }]; 
} 
+0

简单的解决方法,谢谢,发现了一个小问题:当autoreverse动画完成时,按钮突然从1.5x调整为1x。可以稍微修改来避免,单独发布完整的代码。 – Thiru 2015-02-12 09:37:11

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]; 
}]; 

}

0

斯威夫特:

button.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)