2012-04-21 150 views

回答

13

這是一個簡單的解決方案,您的問題。

聲明

@interface ViewController : UIViewController 
{ 
    IBOutlet UILabel * result;  
    NSTimer * timer;     
    int currentTime; 
} 

- (void)populateLabelwithTime:(int)milliseconds; 
-(IBAction)start; 
-(IBAction)pause; 
@end 

定義

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    currentTime = 270000000; // Since 75 hours = 270000000 milli seconds 


} 

-(IBAction)start{ 
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES]; 

} 
-(IBAction)pause{ 
    [timer invalidate]; 

} 
- (void)updateTimer:(NSTimer *)timer { 
    currentTime -= 10 ; 
    [self populateLabelwithTime:currentTime]; 
    } 
- (void)populateLabelwithTime:(int)milliseconds { 
    int seconds = milliseconds/1000; 
    int minutes = seconds/60; 
    int hours = minutes/60; 

    seconds -= minutes * 60; 
    minutes -= hours * 60; 

    NSString * result1 = [NSString stringWithFormat:@"%@%02dh:%02dm:%02ds:%02dms", (milliseconds<[email protected]"-":@""), hours, minutes, seconds,milliseconds%1000]; 
    result.text = result1; 

} 
+1

+1了詳細的解釋。保持。 – 2012-04-21 09:14:57

+0

海它工作正常,有一些變化...我有滾動視圖,其中我顯示的時間..如果我拖動滾動視圖時間不計算(在背景也)...我必須做的。 ..? – SriKanth 2012-04-21 12:33:01

+1

添加此代碼以在滾動期間啓用計時器 ........................................ .......................................... - (IBAction)start { timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer :) userInfo:nil repeatats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } – 2012-04-22 06:15:54

3

使用縮短定時器下面的代碼顯示的時間。

double dblRemainingTime=60; //1 minute 

if(dblRemainingTime >0) 
{ 
    dblRemainingTime -= 1; 
    int hours,minutes, seconds; 
    hours = dblRemainingTime/3600; 
    minutes = (dblRemainingTime - (hours*3600))/60; 
    seconds = fmod(dblRemainingTime, 60); 
    [lblRemainingTime setText:[NSString stringWithFormat:@"%02d:%02d",minutes, seconds]]; 
} 
else 
     alert('Time up buddy');