2012-09-14 29 views
0

0在10秒內有一個標籤,該標籤顯示得分爲10000〜0 10秒(lblPoints)更新一個標籤,顯示10000使用的NSTimer

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

誰能告訴我怎麼可以顯示10000〜 0準確而精確地10秒?

回答

3

顯示屏僅在60 Hz最大更新和的NSTimer射擊精度取決於發生了什麼UI運行循環,因此可能有一個到幾個顯示幀時間(超過10 mS)的錯誤,當然不是1 mS分辨率。由於設備最多隻能在10秒內顯示600個獨特幀,所以10000和0之間的大量數字將被跳過。

我會嘗試使用CADisplayLink設置爲60 Hz顯示更新(這是最大值),使用mach_time檢查當前時間,從某個開始時間中減去該值,從10.0中減去該值以減少計數,並顯示1000X以秒爲單位的差值,直到達到0.0秒。

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

我希望這會幫助你隊友..

:)

+0

我想這不會工作!因爲你的建議10.0爲間隔。因爲它每10秒只會減少-1。它反對質疑。你應該更新你的答案。 – Hemang

+0

這將調用方法pointstimermethod每10秒,我想調用這個方法faslty,它可以在10秒內顯示10000到0 – Anand

+0

嘿夥計在scheduledTimerWithTimeInterval這裏:10.0你定義秒爲10.0如果你想快速然後descrese它與1.0像scheduledTimerWithTimeInterval:1.0 –

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

+0

感謝您的回覆,但是,這是我已經在做的,如果你可以看到我的問題,並且我也指定了10000也 – Anand

+0

是的,但是你沒有使計時器無效,一旦它達到零。否則,你的問題是什麼? – Ilanchezhian

+0

我不能使計時器失效,因爲我不得不重複使用它,事實上我想顯示這10000到0顯示每10秒,我必須做這60到0秒 – Anand

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