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

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

问题描述

在我们的应用中,我们在 CAEAGLLayer 上方有 UIScrollView.UIScrollView 包含一些 UIViews(红色矩形).在 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 中绘制一组白色方块并为其设置动画,并为一组红色 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天全站免登陆