2012-01-04 112 views
2

我想在我的iOS應用程序中加載視圖後爲背景設置動畫。這適用於純色,但我無法讓它使用背景圖像。這是我的代碼:iOS中的動畫背景

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [UIView animateWithDuration:5.0 animations:^{ 
     self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]]; 
     self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background_dark.png"]]; 
    }]; 

    [UIView commitAnimations]; 
} 

有沒有人知道正確的方法來做到這一點?

謝謝

回答

3

不是所有的屬性都可以像這樣的動畫。背景圖像很可能就是其中之一。你可以做的是對上面的第二圖以新形象,其alpha動畫從0到1

0

泰這樣的:

[UIView animateWithDuration:5.0 animations:^{ 
     self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]]; 
    } completion:^(BOOL finished) 
    { 
     self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background_dark.png"]]; 
    }]; 
16

您動畫這樣的事情:

view.backgroundColor = INITIAL COLOR 
[UIView animateWithDuration:5.0 animations:^{ 
    view.backgroundColor = FINAL COLOR 
}]; 

animate方法將動畫從當前狀態改變爲您在動畫塊中設置的任何內容。動畫塊不是一個步驟列表。

因此,我認爲,你想要的是這樣的:

self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]]; 
[UIView animateWithDuration:5.0 animations:^{ 
    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background_dark.png"]]; 
}]; 

另外,不要叫commitAnimations後,只有當你使用老式的方法beginAnimations的。閱讀文檔,不要只是調用方法來查看它們是否工作。

+0

事實上,老式的動畫有時會泄漏CGContextRef的內存 – 2013-04-24 16:53:05

+0

在我的情況下,我試圖改變背景顏色多次,由於用戶滾動,但它只適用於第一次然後它改變背景沒有動畫,什麼可以是原因嗎? – 2014-04-20 20:41:23