绘制路径 - iPhone
我正在 iPhone 上沿着路径制作一个对象的动画。该代码使用CAKeyframeAnimation
效果很好。出于调试目的,我想在屏幕上画线。我似乎无法使用当前的 CGContextRef 来做到这一点。我到处都看到它说你需要子类化 UIView 然后重写 drawRect 方法才能在屏幕上绘制。这附近还有吗?如果没有,我如何将数据传递给drawRect方法以便它知道要绘制什么?
编辑: 好的。我最终对 UIView 进行了子类化并在 drawRect 方法中实现了绘图。我可以通过在子类视图中创建另一个方法(称为drawPath)来将其绘制到屏幕上,该方法设置实例变量然后调用setNeedsDisplay。这反过来会触发使用实例变量绘制到屏幕上的drawRect方法。这是最佳实践吗?如果我想绘制 20 多条路径,会发生什么情况?我不必为所有这些创建属性。
I am animating an object along a path on the iPhone. The code works great using CAKeyframeAnimation
. For debugging purposes, I would like to draw the line on the screen. I can't seem to do that using the current CGContextRef
. Everywhere I look it says you need to subclass the UIView then override the drawRect method to draw on the screen. Is there anyway around this? If not, how do I pass data to the drawRect method so it knows what do draw?
EDIT:
Ok. I ended up subclassing UIView and implementing the drawing in the drawRect method. I can get it to draw to the screen by creating another method in the subclassed view (called drawPath) that sets an instance variable then calls setNeedsDisplay. That in turn fires the drawRect method which uses the instance variable to draw to the screen. Is this the best practice? What happens if I want to draw 20+ paths. I shouldn't have to create properties for all of these.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 UIView 的 drawRect 方法中放置一些像这样的代码
In your drawRect method of UIView put some code like this
如果您想使用 Core Graphics 绘图,请使用 CALayer。如果您不想子类化它,请将绘图委托给视图。
当您需要重绘时,不要忘记调用
[self.layer setNeedsDisplay];
。If you want to use Core Graphics drawing use
CALayer
. If you do not want to subclass it, delegate drawing to the view.Do not forget to call
[self.layer setNeedsDisplay];
when you need to redraw it.