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 t开发者_Python百科he 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.
I would suggest setting up an NSTimer on the main thread to call drawRect every 1.0/30
or 1.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.
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.
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.
精彩评论