2012-04-21 130 views
4

我使用以下代碼顯示時間...我在我的viewController中滾動視圖...在這裏,我顯示的時間以00:00.00開始(mm:ss:SS)(分鐘:秒:毫秒)和目標遞增毫秒,基於毫秒的秒,基於秒的分鐘...但我想顯示從75:00.00(毫米:SS:SS)開始的時間,我想遞減毫秒,秒和分鐘00:00.00(毫米:ss.SS)...我怎麼能..?NStimer在iOS中無法正常工作

我已經問這所以在下面的鏈接... NSTimer Decrease the time by seconds/milliseconds

我我遵循代碼的時間不計算(在後臺也)當我拖動,並與出釋放滾動視圖按鍵...幫我在下面的代碼的變化...

enter code here 
@interface ViewController : UIViewController 
{ 
    IBOutlet UILabel *time; 
    NSTimer *stopWatchTimer; 
    NSDate *startDate; 
    NSTimeInterval secondsAlreadyRun; 
} 

- (void)reset:(id)sender; 
- (void)onStartPressed:(id)sender; 
- (void)onStopPressed:(id)sender; 


enter code here 
-(void)showActivity:(NSTimer *)tim 
{ 
    NSDate *currentDate = [NSDate date]; 

    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    // Add the saved interval 
    timeInterval += secondsAlreadyRun; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    static NSDateFormatter * dateFormatter = nil; 
    if(!dateFormatter){ 
     dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"mm:ss.SS"]; 
     [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    } 
    NSString *timeString=[dateFormatter stringFromDate:timerDate]; 
    time.text = timeString; 

    // [dateFormatter release]; 
} 

- (void)onStartPressed:(id)sender 
{ 
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 
                 target:self 
                selector:@selector(showActivity:) 
                userInfo:nil 
                repeats:YES]; 
    // Save the new start date every time 
    startDate = [[NSDate alloc] init]; // equivalent to [[NSDate date] retain]; 
    [stopWatchTimer fire]; 
} 

- (void)onStopPressed:(id)sender 
{ 
    // _Increment_ secondsAlreadyRun to allow for multiple pauses and restarts 
    secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate]; 
    [stopWatchTimer invalidate]; 
    stopWatchTimer = nil; 

    // [startDate release]; 
    // [self showActivity:stopWatchTimer]; 
} 

回答

11

您必須先註冊計時器在NSRunLoopCommonModes

+0

也相關(和我最大的gottcha):每個線程都有自己的RunLoop對象。 [[NSRunLoop currentRunLoop]會給你當前線程的runloop對象;並根據你的代碼,如果你不在主線程上安排你的計時器,你的計時器可能永遠不會打勾。 – pretzels1337 2012-09-21 19:51:44

1
IBOutlet UILabel * result; 
NSTimer * timer;     
int currentTime; 


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

.m文件

- (void)viewDidLoad 
{ 
    currentTime = 270000000; // Since 75 hours = 270000000 milli seconds 
    // ..... some codes.... 
} 
- (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; 

} 
+0

海潤...我已經試過這個代碼...我已經在滾動型我的視圖控制器,我顯示在視圖控制器的時間。 。如果拖動並按住滾動視圖的時間不計算(在背景也)...我怎麼能? – SriKanth 2012-04-21 13:08:45

+0

謝謝!它爲我工作.... – Aayushi 2017-06-01 12:06:49

5

定時器在某些運行循環模式下的運行循環中進行安排。只有當運行循環以其中一種模式運行時,它們纔會觸發。 +scheduledTimerWithTimeInterval:...方法在默認模式下調度定時器。在滾動條被操縱時跟蹤事件使用NSEventTrackingRunLoopMode。您需要將計時器安排在適當的模式下。

您可以安排在NSRunLoopCommonModes這是一組虛擬模式。運行循環永遠不會以這種模式運行,但它們運行在該組的成員模式下。默認模式和NSEventTrackingRunLoopMode被添加到該集,因爲是NSModalPanelRunLoopMode