对于核心图绘制,drawrect比在移动的触摸中使用核心图形更有效吗?

问题描述:

我的整个应用程序使用touchesMoved进行绘图(Coregraphics).我应该从touchesMoved内部调用[self setNeedsDisplay]而不是在其中执行绘制代码吗?这样可以停止我遇到的一些滞后吗?我没有在发布前尝试此操作的唯一原因是因为将所有图形都转换到drawRect之内将花费我几个小时,而且我想知道是否比在touchesMoved中进行绘制更有效?

My whole app uses touchesMoved for drawing (Coregraphics). Should I be calling [self setNeedsDisplay] from within touchesMoved instead of performing the drawing code in there? Would that stop some lag that I am experiencing? The only reason I didn't try this before posting is because it is going to take me a few hours to convert all drawing to within drawRect and I wanted to know if it would be more would efficient than drawing in touchesMoved?

是.您应该始终将图形保留在drawRect中,并使用setNeedsDisplay触发实际图形.这是在Cocoa Touch中渲染视图的关键.

Yes. You should always keep your drawing in drawRect and use setNeedsDisplay to trigger the actual drawing. This is key to the rendering of views in Cocoa Touch.

例如,如果您不使用此方法,则可能会将绘图代码散布在类中的多个方法中.此外,这还可以确保您的绘图代码在渲染周期内仅执行一次.本质上,调用setNeedsDisplay会触发一个无效标志,该标志使您的UIView知道自己重绘.如果没有此功能,由于渲染周期,您可能会执行实际上不需要的额外绘制操作.

For example, if you didn't use this method, you could potentially have drawing code scattered around your class in multiple methods. In addition, this also ensures that your drawing code executes only once during a render cycle. Essentially, calling setNeedsDisplay triggers an invalidation flag that lets your UIView know to redraw itself. Without this, you could be performing extra drawing operations that aren't actually needed due to the render cycle.