简体   繁体   English

当UITableView滚动时,UITableViewCell动画停止

[英]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. 我正在我的应用程序中创建一个“事件倒计时”场景,该场景使用UITableViewController来显示将来发生的事件列表。 In each cell I have a countdown timer that counts down the time until the event takes place. 在每个单元格中,我有一个倒数计时器,它会计算事件发生前的时间。

在此输入图像描述

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. 我已经实现了这个动画,但问题是当用户滚动uitableview时动画暂停,直到用户释放滚动。 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). 我动画的方式是每隔0.25秒在NSTimer上调用[self.tableview reloadData](我知道这不是最有效的方法 - 这更像是测试动画本身)。 I have also tried running an NSTimer calling view updates within the UITableViewCell itself - but still have the animation pauses when scrolling. 我还尝试在UITableViewCell本身内运行NSTimer调用视图更新 - 但滚动时仍然会暂停动画。

Anyone know a setting or alternative way of calling the animation that will keep it going whilst scrolling? 任何人都知道调用动画的设置或替代方式,以便在滚动时保持它的运行状态?

Some more searching on this same issue for UIScrollViews yielded led me to this post My custom UI elements... 更多关于UIScrollViews的同一问题的搜索引发了我的帖子我的自定义UI元素......

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. 基本上发生的事情是我正在使用的NSTimer在滚动时没有被更新,因为滚动阻止了它自己的运行循环模式之外的所有内容。 The fix is to add the animation NSTimer to the same run loop mode that is used by scrolling: NSRunLoopCommonModes 修复是将动画NSTimer添加到滚动使用的相同运行循环模式: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];

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). 而是创建倒计时模型,可以使用开始日期(NSDate)和持续时间(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. 当一个tableview单元格在视图中变得可见时,它将从相应的Countdown对象加载数据,一切都应该顺利运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM