如何将OpenGL绘图与UIKit更新同步 [英] How to synchronize OpenGL drawing with UIKit updates

查看:88
本文介绍了如何将OpenGL绘图与UIKit更新同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用中,我们在CAEAGLLayer上方具有UIScrollView. UIScrollView包含一些UIView(红色矩形).在CAEAGLLayer中,我们绘制白色矩形.白色矩形的中心与红色矩形的中心相同.当UIScrollView滚动时,我们将更新CAEAGLLayer中白色矩形的位置并进行渲染.

In our app we have UIScrollView above CAEAGLLayer. UIScrollView contains some UIViews (red rectangles). In CAEAGLLayer we draw white rectangles. Centers of white rectangles are the same as the centers of red rectangles. When UIScrollView scrolls, we update positions of white rectangles in CAEAGLLayer and render them.

我们正在获得预期的结果:白色矩形的中心始终与红色矩形的中心相同.

We are getting the expected result: centers of white rectangles are always the same as centers of red rectangles.

但是我们不能将CAEAGLLayer的更新与UIScrollView中视图的移动同步. 我们有一种迷惑-红色矩形落后于白色矩形.

But we can't synchronize updates of the CAEAGLLayer with the movement of the views inside UIScrollView. We have some kind of mistiming – red rectangles lag behind white rectangles.

粗略地说,我们真的想使CAEAGLLayer与UIScollView滞后.

Speaking roughly, we really want to make CAEAGLLayer lag together with the UIScollView.

我们已经准备了示例代码.在设备上运行它并滚动,您将看到白色矩形(由OpenGL绘制)的移动速度快于红色矩形(常规UIViews). OpenGL正在scrollViewDidScroll:委托调用中进行更新.

We have prepared sample code. Run it on the device and scroll, and you will see that white rectangles (drawn by OpenGL) are moving faster than red ones (regular UIViews). OpenGL is being updated within scrollViewDidScroll: delegate call.

https://www.dropbox.com/s/kzybsyu10825ikw/ios-opengl-scrollview-test.zip

即使在iOS模拟器中,它的行为也一样,只需看一下视频即可: http://www.youtube.com/watch?v=1T9hsAVrEXw

It behaves the same even in iOS Simulator, just take a look at the video: http://www.youtube.com/watch?v=1T9hsAVrEXw

红色= UIKit,白色= OpenGL

Red = UIKit, White = OpenGL

代码是:

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    // reuses red squares that are gone outside thw bounds
    [overlayView updateVisibleRect:CGRectMake(...)];
    // draws white squares using OpenGL under the red squares
    [openGlView updateVisibleRect:CGRectMake(...)];
}

可以通过简化的示例轻松演示同一问题.可以在以下位置找到有效的xcodeproj:

The same issue can easily be demonstrated in a much simplified sample. The working xcodeproj can be find at:

https://www.dropbox.com/s/vznzccibhlf8bon/simple_sample.zip

该示例项目基本上在OpenGL中绘制并设置了一组WHITE正方形的动画,并为一组RED的UIView执行了相同的操作.在红色和白色方块之间很容易看到滞后现象.

The sample project basically draws and animates a set of WHITE squares in OpenGL and does the same for a RED set of UIViews. The lagging can easily be seen between the red and white squares.

推荐答案

实际上,您不能使用当前的API对其进行同步. MobileMaps.app和Apple Map Kit使用CAEAGLLayer asynchronous上的私有属性来解决此问题.

In fact, you can't synchronize them using current APIs. MobileMaps.app and Apple Map Kit use private property on CAEAGLLayer asynchronous to workaround this issue.

这里是相关的雷达: http://openradar.appspot.com/radar?id= 3118401

这篇关于如何将OpenGL绘图与UIKit更新同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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