与在触摸移动中使用核心图形相比,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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。您应该始终将绘图保留在drawRect中并使用setNeedsDisplay来触发实际绘图。这是 Cocoa Touch 中视图渲染的关键。
例如,如果您不使用此方法,则绘图代码可能会分散在类的多个方法中。此外,这还确保您的绘图代码在渲染周期中仅执行一次。本质上,调用 setNeedsDisplay 会触发一个失效标志,让您的 UIView 知道要重绘自身。如果没有这个,您可能会执行由于渲染周期而实际不需要的额外绘图操作。
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.
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.
您不应该在 UI 回调内进行绘图。它会减慢或冻结 UI 响应能力,并且如果您想直接绘制到 UIView 中,您将没有适当的绘图上下文。
相反,可以将稍后绘制所需的信息保存、安排或排队,或者保存到您自己的位图绘制上下文中,或者在该视图的 drawRect 回调期间。
根据所需的帧速率,您甚至不需要对每个 TouchMoved 事件执行 setNeedsDisplay,而是可以对数据进行排队并为动画速率计时器或 CADisplayLink 回调设置标志,以更恒定的速率执行 setNeedsDisplay(仅说每秒 15 或 30 帧)。这也将有助于消除额外的绘图操作。
You should not do drawing inside a UI callback. It will slow down or freeze the UI responsiveness, and you won't have the proper drawing context if you want to draw directly into a UIView.
Instead save, schedule or queue the information required for drawing for later, either into your own bitmap drawing context, or during the drawRect callback for that view.
Depending on the frame rate required, you many not even need to do a setNeedsDisplay on every touchesMoved event, but can queue the data and set a flag for an animation rate timer or CADisplayLink callback to do the setNeedsDisplay at a more constant rate (say only 15 or 30 frames per second). This will also help eliminate extra drawing operations.