使用 Core Graphics 以 30-60 fps 手动移动/旋转物体
我有一个循环在一个单独的线程中运行,更新内容的位置和旋转,它以足够的 FPS 运行,并最终在主线程上调用 setNeedsDisplay。问题是框架调用drawRect的次数不够,平均每秒调用10次drawRect(FPS)。我尝试调用 CATransactionlush 但似乎根本没有效果。
我正在做的绘图是 CGContextShowTextAtPoint。在旋转上下文等等之后,就差不多了。有什么办法可以让它以更高的频率绘制吗?我不想使用 openGL 或 cocos2d 因为绘图代码工作得很好。
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. The problem is that the framework doesn't call drawRect enough times, on average i get 10 drawRect (FPS) calls per sec. I tried calling CATransaction flush but it seems to have no effect at all.
The drawing i'm doing is 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议在主线程上设置一个 NSTimer,每隔
1.0/30
或1.0/60
秒调用一次drawRect。也许在单独线程上运行的代码对整个系统的负荷太大。因此,主线程没有足够的计算资源来经常更新视图。如果从未显示额外的数据帧,那么“绰绰有余的 FPS”可能只是浪费计算。单独的线程是否可以在更新位置和旋转后暂停并等待主线程更新视图,然后再计算下一个位置和旋转? NSTimer 可以向第二个线程发送消息来计算下一帧的数据。
I would suggest setting up an NSTimer on the main thread to call drawRect every
1.0/30
or1.0/60
seconds.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. 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.
调用 setNeedsDisplay 不会导致视图立即重绘,它只是将视图标记为脏,这将导致它在下一个绘制周期重绘。
我认为使用 setNeedsDisplay 方法很难实现快速流畅的动画。
您是否尝试过使用核心动画?
最后一点:如果您确实使用 setNeedsDisplay,请确保始终从主线程调用它。
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.
I think it will be difficult to achieve fast and smooth animations using the setNeedsDisplay method.
Have you tried using core animation instead?
One final note: if you do use setNeedsDisplay make sure you always call it from the main thread.
我最终使用 Cocos2d 进行旋转动画等。似乎使用 Core Graphics 以这些 FPS 速率手动移动东西太痛苦了,并且需要一些黑客方法。
I ended up using Cocos2d for animation the rotations and such. It seems that using Core Graphics to manually move stuff at those FPS rates is too painful and would require hackish approaches.