UIScrollView 滚动时暂停 NSTimer
我有一个 UIScrollView,它有一系列快速更新数字的标签(每 0.06 秒)。然而,当滚动视图移动时,NSTimer
会暂停,直到滚动和弹性动画完成后才会继续。
如何避免这种情况并让 NSTimer
运行而不管滚动视图的状态如何?
I have a UIScrollView
that has a series of labels which are rapidly updating numbers (every .06 seconds). While the scroll view is moving, however, the NSTimer
is paused and does not continue until after the scrolling and the elastic animation have finished.
How can I avoid this and have the NSTimer
run regardless of the state of the scroll view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
已下线请稍等2024-12-06 16:17:45
(Swift) 另一种选择:您可以使用基于 GCD 的计时器系统,如下所示:
class GCDTimer {
private var _timer : dispatch_source_t?
init() {
}
private func _createTheTimer(interval : Double, queue : dispatch_queue_t, block : (() -> Void)) -> dispatch_source_t
{
let timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (timer != nil)
{
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, Int64(interval * Double(NSEC_PER_SEC))), UInt64(interval * Double(NSEC_PER_SEC)), (1 * NSEC_PER_SEC) / 10);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
}
return timer;
}
func start(interval : Double, block : (() -> Void))
{
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = _createTheTimer(interval, queue: queue, block: block)
}
func stop()
{
if (_timer != nil) {
dispatch_source_cancel(_timer!);
_timer = nil;
}
}
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
解决此问题的一个简单方法是将您的
NSTimer
添加到mainRunLoop
中。要从安装了该定时器的所有运行循环模式中删除该定时器,请向该定时器发送一条
invalidate
消息。An easy way to fix this is adding your
NSTimer
to themainRunLoop
.To remove a timer from all run loop modes on which it is installed, send an
invalidate
message to the timer.