4
votes

I am making an 'event countdown' scene in my app that uses a UITableViewController to show a list of events that are happening in the future. In each cell I have a countdown timer that counts down the time until the event takes place.

enter image description here

I want to animate these countdown fields so they tick down in real time. I have achieved this animation already, but the problem is that when the user scrolls the uitableview the animation pauses until the user releases the scroll. I want the animations to continue even when scrolling.

The way I'm animating is by calling [self.tableview reloadData] on a NSTimer every 0.25s (I know this isn't the most efficient method - this was more testing the animation itself). I have also tried running an NSTimer calling view updates within the UITableViewCell itself - but still have the animation pauses when scrolling.

Anyone know a setting or alternative way of calling the animation that will keep it going whilst scrolling?

2
Hrmm, you could just [self.tableview reloadData] every time the view is scrolled... UITableViewDelegate extends UIScrollViewDelegate.nhgrif
Are you reusing the cells?quaertym
@nhgrif would reloading data like this be too expensive computationally?Tys
@quaertym yes I am reusing the cells.Tys
@happlii I don't know. Put it in, run it, and find out.nhgrif

2 Answers

12
votes

Some more searching on this same issue for UIScrollViews yielded led me to this postMy custom UI elements...

Basically what is happening is that the NSTimer I was using was not being updated when scrolling because scrolling blocks everything outside of its own run loop mode. The fix is to add the animation NSTimer to the same run loop mode that is used by scrolling: NSRunLoopCommonModes

Here's how to do it in code:

NSTimer *timer = [NSTimer timerWithTimeInterval:0.25 self selector:@selector(updateCountdownLabels) userInfo:nil repeats:YES]; 
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-1
votes

I am guessing you are using views to store your countdown data. Instead create a model for countdown, it could be initialized with a start date(NSDate) and a duration(NSTimeInterval). Then instead of your views update those Countdown objects according to timer. When a tableview cell becomes visible in the view, it will load data from the corresponding Countdown object and everything should run smoothly.