当UIScrollView滚动时,我的自定义UI元素不会被更新 [英] My custom UI elements are not being updated while UIScrollView is scrolled

查看:165
本文介绍了当UIScrollView滚动时,我的自定义UI元素不会被更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旋转圈的UI元素,由NSTimer更新。我也有一个UIScrollView,将阻止旋转的圆,而滚动。这是预期的,因为定时器和滚动视图,在同一个线程中。

I have a spinning circle UI element that is updated by an NSTimer. I also have a UIScrollView that would 'block' the spinning circle while being scrolled. That was expected, because the timer and the scroll view where both in the same thread.

所以我把定时器在一个单独的线程基本上是伟大的!我可以看到它工作,因为NSLogs即使在滚动时仍然来。

So I put the timer in a separate thread which basically works great! I can see it working because NSLogs keep coming even while scrolling.

但我的问题是,我的旋转圈仍然停止滚动!我怀疑重绘将停止,直到主线程的运行循环(?)的下一次迭代。所以即使它的角度一直在改变,它可能不会重绘...

But my problem is that my spinning circle is still stopping on scrolling! I suspect redrawing is halted until the next iteration of the main thread's run loop (?). So even if its angle is changed all the time, it might not be redrawn...

任何想法我能做什么?感谢!

Any ideas what I can do? Thanks!

推荐答案

在滚动时,主线程的运行循环位于 UITrackingRunLoopMode 。所以你需要做的是在这种模式下调度你的定时器(可能除了默认模式)。我相信NSRunLoopCommonModes包括默认和事件跟踪模式,所以你可以这样做:

While scrolling, the main thread's run loop is in UITrackingRunLoopMode. So what you need to do is schedule your timer in that mode (possibly in addition to the default mode). I believe NSRunLoopCommonModes includes both the default and event tracking modes, so you can just do this:

NSTimer *timer = [NSTimer timerWithTimeInterval:0.42 target:foo selector:@selector(doSomething) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

顺便说一下,你应该在主线程上这样做。不需要产生一个后台线程(除非每个定时器调用都要做一些冗长的处理)。

By the way, you should do this all on the main thread. No need to spawn a background thread (unless each timer invocation is going to do some lengthy processing).

这篇关于当UIScrollView滚动时,我的自定义UI元素不会被更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆