2013-02-12 75 views
0

我想動畫計算向上計算digets的計數。文本是類似於You have driven for 0.0km的UILabel,需要使用計數動畫將其更改爲You have driven for 143.6km。有什麼辦法可以更新動畫?更新UILabel文本動畫

編輯 下面是我的一些當前的代碼,關於其他的動畫我已經有了:

 if (animated) 
     { 
      [UIView beginAnimations:@"scaleAnimation" context:nil]; 
      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
      [UIView setAnimationDuration:animationDuration]; 
     } 

     [...] 

     // Amount pointer 
     float xForRedBar = redBarFrame.size.width + redBarFrame.origin.x; 

     CGRect pointerFrame = cell.amountBarPointer.frame; 
     pointerFrame.origin.x = (xForRedBar - (pointerFrame.size.width/2)); 

     if (pointerFrame.origin.x < 12) 
      pointerFrame.origin.x = 12; 

     if (pointerFrame.origin.x >= (308 - (pointerFrame.size.width/2))) 
      pointerFrame.origin.x = 308 - pointerFrame.size.width; 

     [cell.amountBarPointer setFrame:pointerFrame]; 

     // Amount bar 
     CGRect amountBarFrame = cell.amountBar.frame; 
     amountBarFrame.origin.x = 9+(((302 - amountBarFrame.size.width)/100)*self.procentCompleted); 

     [cell.amountBar setFrame:amountBarFrame]; 

     // Amount info text 
     CGRect amountInfoFrame = cell.amountInfo.frame; 
     amountInfoFrame.origin.x = amountBarFrame.origin.x + 2; 

     [cell.amountInfo setFrame:amountInfoFrame]; 

     // Amount text 
     [cell.amountInfo setText:[NSString stringWithFormat:NSLocalizedString(@"You have driven for %@km", nil), self.userAmount]]; 

     [...] 

     if (self.procentCompleted == 0) 
     { 
      [cell.amountBar setAlpha:0]; 
      [cell.amountBarPointer setAlpha:0]; 
      [cell.amountInfo setAlpha:0]; 
     } 
     else { 
      [cell.amountBar setAlpha:1]; 
      [cell.amountBarPointer setAlpha:1]; 
      [cell.amountInfo setAlpha:1]; 
     } 

     if (animated) 
     { 
      [UIView commitAnimations]; 
     } 
+0

在這裏你去:http://stackoverflow.com/a/6267259/1228534 – graver 2013-02-12 15:01:20

+0

謝謝...但是,這並沒有解決我的問題。 – 2013-02-12 15:07:53

+0

那麼你的問題是如何改變每個'x'毫秒的數字,直到它從值'a'變爲值'b'?答案就是使用'NSTimer',就像下面的@rdelmar建議的那樣。或者你的問題與任何兩個數字之間的變化的實際動畫有關嗎? – Mathew 2013-02-12 17:18:16

回答

1

當然,你可以更新爲「動畫」,但你必須使用重複的計時器,並在每次調用該定時器的選擇器時向其添加一個(或任何您想要的時間間隔)的計數。包含最終值的測試,並在您到達時終止計時器。

編輯後:

如果你想計數的速度朝終點放慢,我不會用一個定時器,我會用performSelector:withObject:afterDelay :.爲了重複它,你可以從它的選擇器中調用這個方法。您需要進行一些測試,看看您是否接近計數結束時間,然後在每次傳遞時爲延遲添加一點時間。事情是這樣的:

-(IBAction)countUp:(id)sender { 
    [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1]; 
} 

-(void)countUpLabel { 
    static float delay = .01; 
    num+= 1; 
    label.text = [NSString stringWithFormat:@"%d",num]; 
    if (num < 40) { 
     [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1]; 
    }else if (num > 35 && num <50) { 
     [self performSelector:@selector(countUpLabel) withObject:nil afterDelay:.1 + delay]; 
     delay += 0.01; 
    } 
} 
+0

聽起來不那麼有效,但我理解你的建議。也許不值得擁有「很高興」的功能。 – 2013-02-13 09:16:38

+0

@PaulPeelen,我不認爲這是特別的內存密集型,而且它是唯一能看到數字增加的方法(如果這就是你想要的)。我不確定「寬鬆」是什麼意思 - 你希望它慢下來,因爲它接近尾聲? – rdelmar 2013-02-13 16:04:26

+0

正確。我希望它在開始時加快速度,並儘可能減慢速度。與'UIViewAnimationCurveEaseInOut'相同。 – 2013-02-14 08:20:56