2011-03-29 63 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"