2011-03-29 104 views
1

在我的Iphone App中,我使用了下面的代码来实现viewDidLoad()方法中的3个图像的动画,并使用左变换来加载此ViewController。但是这些imageView不显示在视图上。UIImageView动画不显示在视图

NSArray *newArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"search1.png"], 
                [UIImage imageNamed:@"search2.png"], 
                [UIImage imageNamed:@"seach3.png"], 
                nil]; 
UIImageView *imageView = [UIImageView alloc]; 
[imageView initWithFrame:CGRectMake(240, 60, 60, 30)]; 
imageView.animationImages = newArray; 
imageView.animationDuration = 0.25; 
imageView.animationRepeatCount = 3; 
[self.view addSubview:imageView]; 
+0

请尝试将代码放在{code}块中以使其更清晰。 – Roger 2011-03-29 16:01:17

+0

明显的问题首先:你是否检查过[UIImage imageNamed:]调用不返回nil?如果第一次做,你会得到一个完全空的数组,所以看到什么都不会是你可能期望的行为。 – Tommy 2011-03-29 16:13:46

回答

4

只有在您告诉它开始之前,动画才会启动。您将需要添加一个命令startAnimating。我还清理了你的alloc/init,并且我添加了一个命令,使图像视图在它不是动画时仍然保持显示。设置动画不会使图像显示。 image属性用于静止图像,而animationImages属性用于UIImageView的动画图像。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 60, 60, 30)]; 
imageView.image = [UIImage imageNamed:@"search3.png"]; //This will be the image that is visible when it is NOT animating. 


imageView.animationImages = newArray; 
imageView.animationDuration = 0.25; 
imageView.animationRepeatCount = 3; 
[imageView startAnimating]; //This will make the animation start 

[self.view addSubview:imageView]; 

最后,您的第三张图片缺少一个r。我想你想@"search.png"而不是@"seach.png"