2013-02-12 5 views
0

위쪽으로 계산되는 텍스트에서 숫자의 집계에 애니메이션을 적용하고 싶습니다. 텍스트는 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

+0

감사합니다 ...하지만 그건 내 문제가 해결되지 않습니다. –

+0

그래서'a' 값에서'b' 값으로 갈 때까지 매 x 밀리 초마다 숫자를 변경하는 방법에 대한 질문이 있습니까? 그 대답은 아래의 @rdelmar가 제안한 것처럼'NSTimer'를 사용하는 것입니다. 아니면 귀하의 질문은 두 숫자 사이의 변경 사항의 실제 애니메이션과 관련이 있습니까? – Mathew

답변

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

소리가 나지 않아 메모리가 효율적으로 사용됩니다 ... 그러나 나는 당신의 제안을 이해합니다. 아마도 "가지고있는 것이 좋다"는 가치가 없을 수도 있습니다. –

+0

@PaulPeelen, 저는 이것이 특히 메모리 집약적이라고 생각하지 않습니다. 그리고 숫자를 확인하는 유일한 방법입니다 (원하는 경우). 나는 당신이 "이완 (easing)"을 의미하는지 확신 할 수 없습니다 - 끝까지 갈수록 속도가 느려지 길 원합니까? – rdelmar

+0

수정. 나는 시작에서 속도를 높이고 끝까지 느려지 길 원한다. 'UIViewAnimationCurveEaseInOut'와 같은 것은 애니메이션에 대한 것입니다. –