2014-03-24 50 views
-2

我有2個圖像一個黃金一個綠色,如何在比分改變時替換圖像視圖上的圖像?

當分數爲100時,圖像必須只是黃金圖像,如果分數低於100分,圖像必須更改爲綠色圖像。

} 

if (score < 100) { 
    closeonechange.text = @"Correct!"; 

} else { 
    closeonechange.text = @"Perfect!"; 
} 

綠色的圖像稱爲greenOne.png

這是如何完成的?

+0

你改變了'UIImageView'圖像..你的代碼沒有任何 – Shubhank

回答

0

如果你有兩個UIImageView是你可以用自己的hidden財產隱藏/顯示他們的要求:

if (score < 100) { 
    closeonechange.text = @"Correct!"; 
    imageView1.hidden = NO; 
    imageView2.hidden = YES; 

} else { 
    closeonechange.text = @"Perfect!"; 
    imageView1.hidden = YES; 
    imageView2.hidden = NO; 
} 

如果你有一個UIImageView,你可以在飛行中改變自己的形象:

if (score < 100) { 
    closeonechange.text = @"Correct!"; 
    imageView.image = [UIImage imageNamed:@"correct"]; 

} else { 
    closeonechange.text = @"Perfect!"; 
    imageView.image = [UIImage imageNamed:@"perfect"]; 
} 
+0

謝謝!我用第二個例子,它好多了! – user3449971

0

嘗試這樣:

UIImage scoreImage; 
if (score < 100) { 
    closeonechange.text = @"Correct!"; 
scoreImage = [UIImage imageNamed:@"yellowImage"]; 

} else { 
    closeonechange.text = @"Perfect!"; 
scoreImage = [UIImage imageNamed:@"greenOne"]; 
} 

[scoreImageView setImage:scoreImage]; 
0

首先定義一個UIImageView像這樣UIImageView *imageView = [UIImageView alloc];
並在你的方法中使用如下代碼。

if (score < 100) { 
    closeonechange.text = @"Correct!"; 
    imageView.image= [UIImage imageNamed:@"greenOne.png"]; 
} else { 
    closeonechange.text = @"Perfect!"; 
    imageView.image= [UIImage imageNamed:@"goldOne.png"]; 
}