问题描述
我有一个GLKViewController来处理一些OpenGL绘图.我同时实现了glkView:drawInRect
和update
方法,并且将preferredFramesPerSecond
属性设置为30(默认值).
I have a GLKViewController to handle some OpenGL drawing. I have the glkView:drawInRect
and update
method both implemented and have the preferredFramesPerSecond
property set to 30 (the default).
问题在于,当用户与应用程序的其他部分进行交互时,委托方法将停止触发.我看到的两种情况是用户滚动UITableView或与MKMapView进行交互.
The problem is that the delegate methods stop firing when the user interacts with other part of the app. The two cases that I have seen this happen on is when the user scrolls a UITableView or interacts with a MKMapView.
有没有一种方法可以确保这些委托总是 触发,而不管应用程序其余部分在做什么.我唯一希望这些停止的时间是当应用程序进入后台时(这是自动完成的).
Is there a way to make sure these delegates always fire, regardless of what the rest of the app is doing. The only time I want these to stop is when the app enters the background (which is does automatically).
推荐答案
原因是在表视图或地图视图中滚动时,运行循环位于UITrackingRunLoopMode
中,其优先级高于默认模式.这样可以防止触发某些事件以确保较高的滚动性能.
The reason for this is that during scrolling in a table view or map view the runloop is in UITrackingRunLoopMode
which has a higher priority than the default mode. This prevents some events from firing in order to guarantee a high scrolling performance.
要解决您的问题,您必须设置自己的渲染循环,而不要依赖GLKViewController
.
To solve your problem you must set up your own rendering loop instead of relying on the GLKViewController
.
- 首先将
GLKView
中的enableSetNeedsDisplay
设置为NO
(使用GLKViewController时应自动设置). - 将
preferredFramesPerSecond
设置为0(或可能为1)以禁用GLKViewController
的呈现循环或完全不使用GLKViewController - 导入QuartzCore框架:
#import <QuartzCore/QuartzCore.h>
- 创建一个
CADisplayLink
并将其安排在NSRunLoopCommonModes
中:
- First set
enableSetNeedsDisplay
of theGLKView
toNO
(should be set automatically when using GLKViewController). - set
preferredFramesPerSecond
to 0 (or maybe 1) to disable the rendering loop ofGLKViewController
or don't use GLKViewController at all - Import the QuartzCore framework:
#import <QuartzCore/QuartzCore.h>
- create a
CADisplayLink
and schedule it inNSRunLoopCommonModes
:
CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
- 可选:将displayLink的frameInterval设置为2(=帧速率的一半)
- render方法:
- (void)render:(CADisplayLink*)displayLink {
GLKView* view = (GLKView*)self.view;
[view display];
}
我还没有测试过,所以告诉我是否可行!
I haven't tested this, so tell me if it works!
这篇关于GLKViewControllerDelegate被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!