简体   繁体   English

使用 Core Graphics 以 30-60 fps 手动移动/旋转物体

[英]Manually moving/rotating stuff at 30-60 fps using Core Graphics

I've a got a loop running at a separate thread updating the location and rotation of stuff, it runs at more than enough FPS and calls setNeedsDisplay on the main thread in the end.我有一个循环在一个单独的线程上运行,更新了东西的位置和旋转,它以足够多的 FPS 运行,最后在主线程上调用 setNeedsDisplay。 The problem is that the framework doesn't call drawRect enough times, on average i get 10 drawRect (FPS) calls per sec.问题是框架调用 drawRect 的次数不够多,平均我每秒调用 10 个 drawRect (FPS)。 I tried calling CATransaction flush but it seems to have no effect at all.我尝试调用 CATransaction 刷新,但它似乎根本没有效果。

The drawing i'm doing is CGContextShowTextAtPoint.我正在做的绘图是 CGContextShowTextAtPoint。 That's pretty much it, after rotating the context around and such.差不多就是这样,在旋转上下文等之后。 Is there any way for me to make it draw at a higher frequency?我有什么办法让它以更高的频率绘制吗? I wouldn't want to use openGL or cocos2d because the drawing code work pretty good.我不想使用 openGL 或 cocos2d 因为绘图代码工作得很好。

I would suggest setting up an NSTimer on the main thread to call drawRect every 1.0/30 or 1.0/60 seconds.我建议在主线程上设置一个 NSTimer 以每1.0/301.0/60秒调用一次 drawRect。

Maybe the code running on the separate thread is working the overall system too hard.也许在单独线程上运行的代码使整个系统工作得太辛苦了。 Therefore the main thread doesn't get enough compute resources to update the view often enough.因此,主线程没有获得足够的计算资源来足够频繁地更新视图。 The "more than enough FPS" may be just wasted calculations if the extra frames of data are never shown.如果从未显示额外的数据帧,“足够多的 FPS”可能只是浪费计算。 Can the separate thread pause after updating the location and rotation and wait for the main thread to update the view before calculating the next location and rotation?单独的线程可以在更新位置和旋转后暂停并等待主线程更新视图,然后再计算下一个位置和旋转吗? The NSTimer can send a message to the second thread to calculate the next frame's data. NSTimer 可以向第二个线程发送消息以计算下一帧的数据。

Calling setNeedsDisplay does not cause the view to be redrawn immediately, it just marks the view as dirty which will cause it to be redrawn at the next drawing cycle.调用 setNeedsDisplay 不会导致视图立即重绘,它只是将视图标记为脏,这将导致它在下一个绘图周期被重绘。

I think it will be difficult to achieve fast and smooth animations using the setNeedsDisplay method.我认为使用 setNeedsDisplay 方法很难实现快速流畅的动画。

Have you tried using core animation instead?您是否尝试过使用核心 animation 代替?

One final note: if you do use setNeedsDisplay make sure you always call it from the main thread.最后一点:如果你使用 setNeedsDisplay 确保你总是从主线程调用它。

I ended up using Cocos2d for animation the rotations and such.我最终将 Cocos2d 用于 animation 旋转等。 It seems that using Core Graphics to manually move stuff at those FPS rates is too painful and would require hackish approaches.似乎使用 Core Graphics 以这些 FPS 速率手动移动东西太痛苦了,并且需要一些骇人听闻的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM