2012-11-09 38 views
0

我必須嘗試添加一個按鈕,該按鈕在主視圖上以編程方式從0不透明度動畫到完全不透明度。這是什麼編碼?我被給了這個編碼,但似乎無法使它工作。如何以編程方式添加從0不透明度到完全不透明度的動畫按鈕

[UIElement setAlpha: 0.0f]; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 

[UIElement setAlpha: 1.0f]; 

[UIView commitAnimations]; 
+0

什麼是'UIElement'? – taskinoor

+0

hm你是否正在編碼? 因爲Xcode標籤,我猜C,C++或Objective-C。 – Vloxxity

+0

是的,這就是我不是UIElement的東西甚至不是一個大聲笑 在XCODE 4編碼Im編碼 –

回答

1

把這個代碼在您的viewDidLoad方法:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btn.frame = CGRectMake(0,0,100,50); 
btn.alpha = 0; 
[self.view addSubview:btn]; 

[UIView animateWithDuration: 0.3 
       animations: ^(void) { btn.alpha = 1; } ]; 
+0

確定按鈕顯示,但它不從0 - 100%不透明度生成動畫 –

+0

將動畫持續時間增加到1.0,因此您將有時間查看更改。或者把這段代碼放在viewDidAppear方法中。 –

+0

非常感謝你! –