2012-04-21 5 views
4

다음 코드를 사용하여 시간을 표시하고 있습니다 ... 내 viewController에서 스크롤보기가 있습니다 ... 00 : 00.00 (mm : ss : SS) (분)으로 시작하는 시간을 표시합니다. : seconds : milliseconds) 밀리 초를 기준으로 밀리 초, 밀리 초를 기준으로 초를 증가시키는 것을 목표로합니다.하지만 75 : 00.00 (mm : ss : SS)부터 시작하여 밀리 초, 초 분 : 00 : 00.00 (mm : ssS) ... 어떻게 ..? 내가 드래그 아웃이 해제로 스크롤 뷰를 개최 할 때NStimer가 iOS에서 제대로 작동하지 않습니다.

이미 다음 링크에서 SO이를 요청했습니다 .. 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

또한 관련성이 높은 (내 가장 큰) : 각 스레드는 자체 RunLoop 개체가 있습니다. [[NSRunLoop currentRunLoop]은 현재 스레드의 runloop 객체를 제공합니다. 그리고 코드에 따라 주 스레드에서 타이머를 예약하지 않으면 타이머가 절대로 틱하지 않을 수 있습니다. – pretzels1337

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

하이에서 실행되도록 타이머를 등록해야 ... 내가 ...이 코드를 내보기 컨트롤러에있는 ScrollView를 한 시도, 그 뷰 컨트롤러에서 시간을 표시하고있다. 스크롤 뷰를 드래그하고 잡고 있으면 시간이 계산되지 않습니다 (백그라운드에서도) ... 어떻게 .. 할 수 있습니까? – SriKanth

+0

감사합니다 !!! 그게 나를 위해 일했다 .... – Aayushi

5

타이머는 특정 실행 루프 모드에서 런 루프로 예약됩니다. 실행 루프가 해당 모드 중 하나에서 실행될 때만 발동 할 수 있습니다. +scheduledTimerWithTimeInterval:... 메서드는 타이머를 기본 모드로 예약합니다. 스크롤 막대가 조작되는 동안 이벤트를 추적하면 NSEventTrackingRunLoopMode이 사용됩니다. 타이머를 적절한 모드로 예약해야합니다.

가상 모드 집합 인 NSRunLoopCommonModes에 일정을 지정할 수 있습니다. 실행 루프는 절대로 그러한 모드에서 실행되지 않지만 해당 세트의 구성원 인 모드로 실행됩니다. 기본 모드와 NSEventTrackingRunLoopMode은 해당 집합에 추가되며 NSModalPanelRunLoopMode입니다.

관련 문제