0

上向きに計算するテキストの数字のカウントをアニメーション化したいと思います。テキストは似たような UILabelにあり、カウントアニメーションYou have driven for 0.0kmで変更する必要があります。You have driven for 143.6kmアニメーションを更新する方法はありますか?

編集 これは、私がすでに持っている他のアニメーションに関する私の現在のコードの一部です:

        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];
        }
4

1 に答える 1

1

確かに、「アニメーション」で更新できますが、繰り返しタイマーを使用し、タイマーのセレクターが呼び出されるたびにカウントに 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;
    }
}
于 2013-02-12T15:40:39.263 に答える