2012-01-04 111 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