2011-03-16 129 views
3

有沒有可能通過這種方式爲uibutton創建動畫,例如背景中出現的東西或任何東西,例如麥穗從田地裏出來。uibutton動畫

這是可能的嗎?

謝謝:)

+0

因此,您希望麥穗是按鈕,並且該領域是背景圖像? – Ned 2011-03-16 19:02:44

+0

是的,這是正確的:) – Leon 2011-03-16 19:37:26

回答

7

除非你想要一個更復雜的動畫,你可以創建一個自定義類型的UIButton(通過改變類型的自定義或者與IB或編程方式與UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]),然後設置按鈕的圖像與[aButton setImage:[UIImage imageNamed:@"wheat_ear" forState:UIControlStateNormal]

要移動它,只是動畫的位置通常...

[UIView animateWithDuration:0.5 animations:^{aButton.center = newCenter;}]; 

或者提供它的現身現場的假象,動畫圖像的邊界,使其與圖像的開始寬度和零高度,並以圖像的寬度和高度結束:

CGRect originalFrame = aButton.frame; 
aButton.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, originalFrame.size.width, 0); 
[UIView animateWithDuration:0.5 animations:^{aButton.frame = originalFrame;}]; 
+0

你能給我舉一個你最後描述的部分的例子嗎? (從圖像的寬度和零高開始,以圖像的寬度和高度結束)。 – Leon 2011-03-16 20:29:08

+0

添加到我的答案下面。 – Ned 2011-03-17 14:40:31

+0

這是非常有幫助的,但是當我試圖將其作爲書面材料使用時出現錯誤。我需要設置原點和寬度:aButton.frame = CGRectMake(originalFrame.origin.x,originalFrame.origin.y,originalFrame.size.width,0); – DenVog 2012-06-18 20:59:19