2014-10-29 62 views
0

我試圖逐個更改子視圖的顏色,但所有視圖的顏色都立即更改,即使我已將動畫持續時間設置爲2秒。在UIView動畫中調用immediatly的完成塊

- (void)runAnimation 
{ 
    NSMutableArray *views = [[NSMutableArray alloc]init]; 

    for (UIView *bubble in self.subviews) 
    { 
     if(bubble.tag == 2500) 
      [views addObject:bubble]; 
    } 

    __weak PKCustomSlider *weak_self = self; 
    __block NSInteger currentView = 0; 
    self.animationBlock = ^{ 

     [UIView animateWithDuration:2 animations:^{ 
      NSLog(@"Animation block called again"); 
      [views[currentView] setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:1]]; 
     } completion:^(BOOL finished) { 
      currentView += 1; 
      if(!finished) 
      { 
       NSLog(@"Animation hasn't finished"); 
       return; 
      } 

      if (currentView == views.count){ 
       currentView = 0; 
       NSLog(@"Animation block ended"); 
      } 
      else 
      { 
       weak_self.animationBlock(); 
       NSLog(@"Animation block called again because views still available in the array"); 
      } 
     }]; 

    self.animationBlock(); 
} 
+1

你怎麼在日誌中看到了什麼? – jrturton 2014-10-29 15:48:49

+0

我認爲如果視圖不是當前UIWindow的子視圖,則立即執行完成塊。 – 2014-10-29 15:51:44

+0

@ianmacdonald絕對正確 – jrturton 2014-10-29 17:48:06

回答

2

如果您嘗試爲另一個視圖設置動畫效果,則不必依賴完成塊。如果在動畫塊中沒有動畫要完成,則可立即調用完成塊。

你可以做這樣的事情:

- (void)runAnimation 
{ 
    for (int i = 0; i < self.subviews.count; i++) { 
     UIView *bubble = self.subviews[i]; 

     if (bubble.tag != 2500) 
      continue; 

     [UIView animateWithDuration:2 delay:2*i options:0 animations:^{ 
      [bubble setBackgroundColor:[UIColor colorWithRed:0 green:1 blue:0 alpha:1]]; 
     } completion:nil]; 
    } 
} 
+0

這是做問題想要的更好的方法,+1 – jrturton 2014-10-29 17:48:52

+0

對不起,它不工作 – jailani 2014-10-30 06:54:39