2010-12-10 81 views
0

我試圖對UIButton的縮放進行動畫製作,但難以通過動畫以我想要的方式進行動畫製作。僅從底部垂直縮放UIButton

我有一個UIButton設置了可伸縮的UIImage。我希望它的高度能夠縮放到一定的尺寸,並賦予它「增長」的效果。我使用下面的代碼,但它激活了從頂部和底部增長的UIButton,而不僅僅是底部。看到這裏的代碼:

myButton.transform = CGAffineTransformMakeScale(1,0.5); 
myButton.alpha = 0.6f; 
[UIView beginAnimations:@"myButton" context:nil]; 
[UIView setAnimationDuration:0.5]; 
myButton.transform = CGAffineTransformMakeScale(1,1); 
myButton.alpha = 1.0f; 
[UIView commitAnimations]; 

這很難解釋,基本上我想要的UIButton僅從一側(底部),而不是把它擴大雙方成長壯大。有什麼建議麼?


UPDATE

我也試圖改變按鈕的邊框,並且也不能工作。這一次,根本沒有看到動畫。這是我的代碼:

CGRect buttonRect = CGRectMake(0, 0, 150, 22); 
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
myButton.frame = buttonRect; 

myButton.alpha = 0.6f; 
[self.view addSubview:myButton]; 

[UIView beginAnimations:@"myButton" context:nil]; 
[UIView setAnimationDuration:0.3]; 
CGRect buttonRect2 = CGRectMake(0, 0, 150, 52); 
myButton.frame = buttonRect2; 
myButton.alpha = 1.0f; 
[UIView commitAnimations]; 

任何想法,爲什麼它根本不是動畫?我假設它與我如何設置框架有關?

回答

2

,你不會看到任何動畫,直到調用setFrame:方法..

CGRect buttonRect = CGRectMake(0, 0, 150, 22); 
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
myButton.frame = buttonRect; 

myButton.alpha = 0.6f; 
[self.view addSubview:myButton]; 

[UIView beginAnimations:@"myButton" context:nil]; 
[UIView setAnimationDuration:0.3]; 
CGRect buttonRect2 = CGRectMake(0, 0, 150, 52); 
[UIView commitAnimations]; 

[UIView beginAnimations:@"myButton" context:nil]; 
[UIView setAnimationDuration:0.3]; 
myButton.alpha = 1.0f; 
[myButton setFrame: buttonRect2]; 
[UIView commitAnimations]; 
1

最簡單的方法可能是動畫改變frame屬性,並增加寬度和高度,而不是試圖找出圍繞中心點的變換的複雜性。如果您真的需要在此視圖上進行轉換,您可以在動畫完成後始終透明地更新它。

+0

感謝賈斯汀,我試過上方(參見下UPDATE編輯問題),現在它不根本沒有動畫。任何建議,我做錯了什麼?再次感謝! – trx25 2010-12-10 12:46:56

+0

所示的更新看起來正確。有沒有其他地方可以修改`myButton`?代碼看起來像用圖像初始化它的地方是什麼? – 2010-12-10 17:07:11

0

我已經回答了我自己的問題。你需要調用layoutSubviews。 在這裏看到:

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
myButton.frame = CGRectMake(0, 0, 150, 22); 

myButton.alpha = 0.6f; 
[self.view addSubview:myButton]; 
[myButton layoutSubviews]; 

[UIView beginAnimations:@"myButton" context:nil]; 
[UIView setAnimationDuration:0.3]; 
myButton.alpha = 1.0f; 
myButton.frame = CGRectMake(0, 0, 150, 52); 
[myButton layoutSubviews]; 
[UIView commitAnimations];