Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell animations stop when UITableView is scrolling

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?

like image 407
Tys Avatar asked Dec 19 '22 20:12

Tys


1 Answers

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];
like image 162
Tys Avatar answered Mar 03 '23 11:03

Tys