2013-01-08 60 views
1

我是新來的社區,所以讓我知道如果我的問題不清楚。我正在嘗試在iPAD上做出選擇反應練習。有兩個圖像應該以隨機順序出現在屏幕的左側和右側,並且用戶將通過點擊與出現的圖像的位置相對應的按鈕來作出響應。這裏的問題,我試圖讓這兩個圖像使用以下方式以任意順序出現:獲取兩張圖片以隨機順序出現iOS

- (void) viewDidAppear:(BOOL)animated 
{ 
    for(int n = 1; n <= 20; n = n + 1) 
    { 
     int r = arc4random() % 2; 
     NSLog(@"%i", r); 
     if(r==1) 
     { 
     [self greenCircleAppear:nil finished:nil context: nil]; 
     } 
     else 
    { 
     [self redCircleAppear:nil finished:nil context: nil]; 
    } 
    } 
} 

然而,雖然只有1套動畫的運行得到20張產生的隨機數。有沒有辦法讓動畫在下一個循環開始之前在每個循環中完成運行?任何幫助表示感謝,提前致謝!

+0

你怎麼知道的只有1套運行?也許圖像相互之間出現一個隨機數(<20)次 –

+0

只有一組動畫運行時你的意思是什麼 – WaaleedKhan

+0

@Tim因爲在方法redCircleAppear中,動畫設置爲運行2秒,而20個隨機數r出現在同一秒內,所以我推導出只有一組動畫(圓圈出現和消失)運行。理想情況下,我希望有20套出現的用戶回覆 – user1949311

回答

0

當你說「只有一組動畫運行」時,我假設意味着greenCircleAppearredCircleAppear開始出現圖像序列並且用戶按下按鈕。如果是這樣的話,我建議不要在viewDidAppear中使用for循環,而應該用viewDidAppear來初始化當前狀態並調用一個呈現下一個動畫的方法。動畫完成後,讓它調用呈現下一個動畫的方法。沿着這些路線的東西:

一下添加到界面:

@interface ViewController() 

@property NSInteger currentIteration; 

@end 

這是在執行:

- (void)viewDidAppear:(BOOL)animated { 
    self.currentIteration = 0; 
    [self showNextAnimation]; 
} 

- (void)greenCircleAppear:(id)arg1 finished:(id)arg2 context:(id)arg3 { 
    //perform animation 
    NSLog(@"green"); 
    [self showNextAnimation]; 
} 

- (void)redCircleAppear:(id)arg1 finished:(id)arg2 context:(id)arg3 { 
    //perform animation 
    NSLog(@"red"); 
    [self showNextAnimation]; 
} 

- (void)showNextAnimation { 
    self.currentIteration = self.currentIteration + 1; 
    if (self.currentIteration <= 20) { //you should replace '20' with a constant 
     int r = arc4random() % 2; 
     NSLog(@"%i", r); 
     if(r==1) 
     { 
      [self greenCircleAppear:nil finished:nil context: nil]; 
     } 
     else 
     { 
      [self redCircleAppear:nil finished:nil context: nil]; 
     } 
    } 
    else { 
     //do what needs to be done after the last animation 
    } 
} 
+0

Hey @SSteve,這是實現它的好方法。非常感謝您的意見! – user1949311