为什么UIScrollView暂停我的CADisplayLink? [英] Why does UIScrollView pause my CADisplayLink?
问题描述
我有一个由CAEAGLLayer支持的视图,该视图位于UIScrollView内.当我开始滚动时,调用openGL视图的-draw方法的CADisplayLink停止调用.
I have a view backed by a CAEAGLLayer, which is inside a UIScrollView. When I begin scrolling, the CADisplayLink that calls the -draw method of openGL view stops getting called.
我验证了滚动时不会调用我的runloop start/stop方法. -draw方法根本不会在滚动开始后立即被调用,而是在滚动结束后立即被调用.
I verified that my runloop start / stop methods don't get called when scrolling. The -draw method simply doesn't get called as soon as scrolling begins, and resumes getting called as soon as scrolling ends.
UIKit是否会在滚动开始后立即停止CADisplayLink的触发?
Does UIKit stop a CADisplayLink from firing as soon as scrolling starts?
将显示链接添加到运行循环中,如下所示:
The display link is added to the run loop like this:
[dl addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
此运行循环模式和UIScrollView可能存在冲突吗?即使在UIScrollView滚动时,是否还有其他运行循环模式或替代解决方案可保持CADisplayLink触发?
Maybe there is a conflict with this run loop mode and UIScrollView? Are there other run loop modes or alternative solutions to keep a CADisplayLink firing even when a UIScrollView is scrolling?
我认为在任何应用程序中都可以有多个CADisplayLink.错了吗?
I thought there can be more than just one CADisplayLink in any application. Is that wrong?
推荐答案
滚动UIScrollView
时您不在NSDefaultRunLoopMode
中;您在UITrackingRunLoopMode
中.因此,仅为前者安排的任何计时器都不会在后者中触发.您可以通过重复调用addToRunLoop:forMode:
将CADisplayLink
添加到多个运行循环模式,或使用NSRunLoopCommonModes
对其进行一次调用,这涵盖了两种模式.
You're not in NSDefaultRunLoopMode
while scrolling a UIScrollView
; you're in UITrackingRunLoopMode
. So any timer scheduled only for the former won't fire in the latter. You can add your CADisplayLink
to multiple run loop modes by calling addToRunLoop:forMode:
repeatedly, or call it once with NSRunLoopCommonModes
, which covers both modes.
在WWDC 2012的会议223:使用滚动视图增强用户体验"中,他们详细讨论了此问题以及将滚动视图与GL集成的其他问题.我建议您观看视频,因为其中还有许多其他可能与您的情况.
They talked about this in detail, and other issues with integrating scroll views with GL, at WWDC 2012 in Session 223: "Enhancing User Experience with Scroll Views"; I recommend watching the video, as there's lots of other stuff in there that's likely relevant to your situation.
(2016)Swift3中的示例...
An example in (2016) Swift3...
let d = CADisplayLink(target: self, selector: #selector(ThisClassName.updateAlpha))
d.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
//and then, for example...
func updateAlpha() {
let a = leader.layer.presentation()?.value(forKey: "opacity") as! CGFloat
follower.alpha = a
}
这篇关于为什么UIScrollView暂停我的CADisplayLink?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!