为什么 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 启动/停止方法.-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];
可能这个run loop模式和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屋!