Core Graphics- drawRect: not getting called frequently enough
In my application, I have a UIViewController with a subclassed UIView (and several other elements) inside of it. Inside of the UIView, called DrawView, in my drawRect:
method, I draw a table grid type thing, and plot an array of CGPoints on the grid. When the user taps on the screen, it calls touchesBegan:withEvent:
and checks to find the closest point on the grid to the touch, adds a point to the array that the drawRect:
method d开发者_开发知识库raws points from, and calls [self setNeedsDisplay]
. As the user moves their finger around the screen, it checks to see if the point changed from the last location, and updates the point and calls [self setNeedsDisplay]
as necessary.
This works great in the Simulator. However, when run on a real iPhone, it runs very slowly, when you move your finger around, it lags in drawing the dot. I have read that running calculations for where to place the points in a different thread can improve performance. Does anyone have experience with this that knows this for a fact? Any other suggestions to reduce lag?
Any other suggestions to reduce lag?
Yes. Don't use -drawRect:
. It's a long and complicated reason why, but basically when UIKit sees that you've implemented -drawRect:
in your UIView
subclass, rendering goes through the really slow software-based rendering path. When you draw with CALayer
objects and composite views, you can get hardware accelerated graphics, which can make your app FAR more performant.
精彩评论