2011-08-01 6 views
1

가능한 중복 :
UIScrollView pauses NSTimer until scrolling finishes있는 ScrollView 스레드 타이머 아이폰

는 내가있는 ScrollView 현재까지 카운트 다운을 표시하는 라벨이 있습니다. 카운트 다운이 작동하지만 스크롤 뷰를 위아래로 드래그 할 때마다 카운트 다운이 중단되었지만 손가락을 뗀 후에 다시 작동하기 시작했습니다. muti-threading과 관련이 있다는 것을 알고 있습니다. 여기 내 코드는

-(void) viewdidload 
{ 
    timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; 
} 

-(void)updateLabel { 


    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSDateComponents *countdown = [calender 
            components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
            fromDate:[NSDate date] 
            toDate:destDate 
            options:0]; 

    timerCountDown.text = [NSString stringWithFormat:@"%dday %dh %dm %ds", [countdown day],[countdown hour],[countdown minute],[countdown second]]; 
} 

답변

0

다음과 같은 방법으로 텍스트를 UILabel에 설정합니다.

NSString *cntTime = [NSString stringWithFormat:@"%dday %dh %dm %ds", [countdown day],[countdown hour],[countdown minute],[countdown second]]; 
[timerCountDown performSelectorOnMainThread:@selector(setText:) withObject:cntTime waitUntilDone:YES]; 

편집 :

방법은 -(void)viewDidLoad해야한다.

도는 말 1 초

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; 

를 일정 시간 간격을주고 그리고 U는 UIScrollViewDelegate 방법을 구현? 대신 스레드 또는 무엇을하지를 추가하는 모든 문제를 통해가는

+0

시도했지만 작동하지 않았다 : ( – kevin

+0

편집 된 답변 확인 – Ilanchezhian

+0

나는 scrollviewdelegate를 구현하지 않았다 – kevin

1

이 간단하게 : 손가락을 아래로/touchesBegan에

  1. . 기록 또는 놓친 가지고 밀리 초 (A) 손가락에
  2. /touchesEnded (?) 기록 또는 다시 현재 시간을 절약 (B) B에서
  3. 마이너스 A의 현재 시간을 저장 /에 시간을 일시 정지 타이머의 시간에서 빼는 밀리 초.

스크롤 할 때 화면의 시계가 일시 중지되지만 놓으면 곧 다시 업데이트되거나 새로 고침됩니다.

희망 사항은 스레드 방식보다 간단하게 문제를 해결할 수 있기를 바랍니다. LH

0

Nideo의 답변은 좋습니다 (엄격한 카운트 다운을 진행하는 동안 표시되는 것이 무엇인지 예측할 수 있습니다). 그러나 임의로 발생하는 이벤트의 수를 표시해야 할 필요가 있음을 알 수 있습니다. 그렇다면 scrollView 대리자 메서드 scrollViewDidScroll :을 구현하고 소스에서 데이터를 새로 고침 (updateLabel 호출) 할 수 있습니다. UIScrollView를 서브 클래스 화하고 touchesBegan :/touchesMoved :/touchesEnded :를 구현하여 각각의 표시된 값을 새로 고친 다음 이벤트를 super로 전달하여 정상적인 터치 처리를 허용 할 수 있습니다.