开发者

Scrollview thread timer iphone [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

UIScrollView pauses NSTimer until scrolling finishes

I have a开发者_JS百科 scrollview and a label to show the countdown to date. I got the countdown working but whenever I drag the scrollview up and down the countdown stopped but when I release my finger it started working again. I know it has to do with muti-threading. Here is my code

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


Instead of going through all the trouble of adding a thread or what not, make this simple:

  1. On finger down / touchesBegan. Record or save the current time in milliseconds (A)
  2. On finger up / touchesEnded(?) Record or save the current time again (B)
  3. Minus A from B to have the missed/paused time in milliseconds which you then subtract from the timer's time.

Only problem with this is, the clock on the screen will pause while scrolling but as soon as you release it will update or refresh again.

Hope this solves your problem in a way simpler than threads manner. LH


You set the text to the UILabel by the following way.

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

Edit:

The method should be -(void)viewDidLoad.

Also , give some time interval, say 1 sec,

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

And, are u implementing any UIScrollViewDelegate methods?


Nideo's answer is good (as long as you are proceeding in a strict countdown and you can predict what the value of the thing you are displaying will be). However you could find yourself needing to display a count of events that are happening at random. If so you could implement the scrollview delegate method scrollViewDidScroll: and refresh the data from its source (i.e. call updateLabel). You could also subclass UIScrollView and implement touchesBegan:/touchesMoved:/touchesEnded:, refreshing the displayed value in each, then passing the event up to super to allow normal touch handling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜