2013-05-31 34 views
4

正如標題所說,這很奇怪,你同意嗎?UIImageView更改背景顏色和setImage在動畫過程中不起作用

因此,如果有人發現你的代碼

imageView.backgroundColor = [UIColor greenColor]; 

imageView.image = [UIImage imageName:@"angryBird"];" 

不工作。請注意,如果您的imageView是動畫,或者動畫是否已被刪除。

---------- 的回答以下 ---------------

只要停止您設置圖像之前的動畫和改變的背景動畫UIImageView。

UIImageView* imageView = [UIImageView alloc] init]; 
//......do sth. like setFrame ... 
imageView.images = [NSArray arrayWithObjects....]; // set the animation images array 
imageView.animationDuration = 1.0; 
[imageView startAnimating]; 
//......do sth. 
[imageView stopAnimating];  // **** do not forget this!!!!! **** 
imageView.backgroundColor = [UIColor greenColor]; 
imageView.image = [UIImage imageName:@"angryBird"]; 

當你performSelector:withObject:afterDey:1.0秒,在選擇方法,也需要stopAnimating過,然後setImage或改變背景顏色。

回答

3

嘗試更改背景顏色並在動畫完成後更改圖像。它只會爲你工作。

0

試試這個代碼和performSelector:withObject:afterDelay ::這樣

[self performSelector:@selector(animationDidFinish:) withObject:nil 
     afterDelay:instructions.animationDuration]; 

或使用dispatch_after:

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     [self animationDidFinish]; 
    }); 

我希望它可以幫助你

+0

請讓我知道你的代碼是否工作。謝謝 – chandan

+0

在分配圖像名稱爲angryBird之前,請仔細檢查您的拼寫 – chandan

+0

謝謝。這不是框架的問題。在動畫停止之前,我改變了imageView的背景色,最好的方式是在動畫停止後改變背景色。或者,另一種方法是停止其動畫,然後更改背景顏色。 – isaacselement

1

試試這個:

[UIView animateWithDuration:1.0 animations:^(){} completion:^(BOOL finished){}]; 
0

使用塊

[UIView animateWithDuration:1.0 animations:^{ 
    } 
    completion:^(BOOL finished) 
    { 
     [imageView setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME"]]; 
     [imageView setBackgroundColor:[UIColor greenColor]]; 


    }]; 

注試試這個,動畫:這是imageNamed:imageName:,我不知道這是如何被編譯

+0

;),對不起,忘了「imageName:」和「憤怒的小鳥」,我不會在XCode中編寫它。我只是寫在這個文本區域。 – isaacselement

+0

噢好吧,這應該工作嘗試一下 –