2010-09-08 112 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)