0

10秒で10,000から0までのスコアを表示するラベルが1つあります(lblPoints)

    NSTimer pointstimer=[[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(pointstimermethod) userInfo:nil repeats:YES]retain];

    -(void)pointstimermethod
    {
        points=points-1;
        lblPoints.text=[NSString stringWithFormat:@"%d",points];
    }

正確に正確に10秒で10,000から0を表示する方法を誰かに教えてもらえますか?

4

4 に答える 4

3

表示は最大60Hzでのみ更新され、NSTimerの起動精度はUI実行ループで何が起こっているかに依存するため、1 mSの解像度ではなく、1〜数の表示フレーム時間(10 mS以上)のエラーが発生する可能性があります。デバイスは10秒間に最大600の一意のフレームしか表示できないため、10000から0までの多くの数値はスキップされます。

60 Hzの表示更新(最大)に設定されたCADisplayLinkを使用してみて、mach_timeを使用して現在の時刻を確認し、開始時刻からそれを減算し、10.0から減算してカウントダウンを取得し、1000倍の差を秒単位で表示します。 0.0秒に達しました。

于 2012-09-14T06:52:47.577 に答える
1

次の.hファイルを宣言します。

int points;

実装ファイルには、次のコードが含まれています。

-(void)showScore:(id)sender {
    points = 10000;
    [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(pointsTimerMethod:) userInfo:nil repeats:YES];
}

-(void)pointsTimerMethod:(NSTimer*)timer
{
    points=points-1;
    lblPoints.text=[NSString stringWithFormat:@"%d",points];

    if(points == 0) {
        [timer invalidate];
        timer = nil;
    }
}

showScore:10000から0までを表示したい場合は、メソッドを呼び出すだけです。

于 2012-09-14T06:36:44.553 に答える
1

viewDidLoadメソッドで次のコードを貼り付けます

NSTimer pointstimer=[[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(pointstimermethod) userInfo:nil repeats:YES]retain];

    -(void)pointstimermethod
    {
        points=points-1;
        if(points >= 0 && points <= 10000){
              lblPoints.text=[NSString stringWithFormat:@"%d",points];
        }
    }

これがあなたの交尾に役立つことを願っています。

:)

于 2012-09-14T06:33:16.417 に答える
1
NSTimer pointstimer=[[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(pointstimermethod) userInfo:nil repeats:YES]retain];

-(void)pointstimermethod
{
    if(pointstimer==0)
    {
       [pointstimer invalidate]; 
    }
    else
    {
        points=points-1;
        lblPoints.text=[NSString stringWithFormat:@"%d",points];
    }
}
于 2012-09-14T06:38:59.030 に答える