2013-03-04 87 views
1

我有2個圖像,我有4個iamges視圖,我想隨機在2-2圖像視圖上設置這2個圖像。如何在4個不同的圖像視圖上設置兩個圖像?

就像卡game.In用戶選擇1張卡,然後用戶有打開同一個圖像3個選項,如果圖像是相同的,然後用戶接一個地點擊贏得比賽。請幫助我...

int r =arc4random()%4; 
int z=arc4random()%4; 
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:r]; 
imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 

if(r!=z) 
{ 
    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z]; 
    imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 

}else{ 

    if(r==3) 
    { 
     UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z-2]; 
     imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 
    } 
    if(r==0) 
    { 
     UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z+2]; 
     imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 


    }if(r==1) 
    { 
     UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z+2]; 
     imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 
    } 
    if(r==2) 
    { 
     UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z-2]; 
     imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 
    } 

} 
+0

確保標籤是不同 – Dhara 2013-03-04 09:03:51

+0

什麼是「Z」代表? – Apurv 2013-03-04 09:23:11

+0

int z = arc4random()%4; – virant 2013-03-04 09:25:35

回答

0

arc4random()是生成隨機數的函數的名稱。

使用它像arc4random()%3,以生成隨機數。根據隨機數選擇圖像。

您應該生成兩個不同的隨機數。

int r =arc4random()%4; 
int z=arc4random()%4; 

while (r==z) 
{ 
     z = arc4random()%4; 
     if(r != z) 
     break; 
} 

UIImageView *imageView = (UIImageView *)[self.view viewWithTag:r]; 
imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 

UIImageView *imageView = (UIImageView *)[self.view viewWithTag:z]; 
imageView.image=[UIImage imageNamed:@"30-by-30-home"]; 
+0

是的,我做到了,但我得到更迭設置2 imageview的1幅圖像,但我沒有得到成功設置在休息圖像視圖 – virant 2013-03-04 08:32:38

0

對於您使用[self.view viewWithTag:z-2]r==3 & r==2

而且對於這兩個r==0 & r==1您使用[self.view viewWithTag:z+2]

可能是它的原因嗎?

+0

另一個圖像你們有任何的例子嗎?就像在2×2矩陣卡配對遊戲。 – virant 2013-03-04 09:48:59

相關問題