不使用drawrect方法获取UIView的Context
我需要在不使用 drawrect 方法的情况下在 uiview 上绘图,但我不知道如何在任何时间点动态获取图形上下文?
例如:当用户从工具栏中的弹出窗口中选择添加行时,我需要添加一行。
I need to draw on my uiview without using the drawrect method but i don't know how to get the graphics context dynamically at any point of time?
For example: I need to add a line when user selected the add line from from pop over in my toolbar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的。您必须使用某种标记来指示新行,然后调用
setNeedsDisplay
方法,以便它调用drawRect:
方法。您可以在drawRect:
方法中检查标记并相应地绘制线条。您必须保存状态。可能添加了一组行,以便您可以在调用
drawRect:
方法时使用它来绘制视图。替代方案
您可以使用图像视图来实现此目的。您可以这样做,
这样您就不会拥有状态,而只需将线条添加到图像中即可。
This can't be done. You will have to have some kind of marker to indicate a new line and then call
setNeedsDisplay
method so that it invokes thedrawRect:
method. You can check for the marker in thedrawRect:
method and draw the line accordingly.You will have to save state. Probably an array of lines added so that you can use it to draw the view when
drawRect:
method is invoked.An alternative
You can use an image view for this purpose. You can do this,
This way you won't have state but you simply add the line to the image.
您的视图在
drawRect:
之外没有任何图形上下文。您不能在drawRect:
之外进行任意绘图调用并让它们持续存在。如果您需要动态地将线条等添加到视图中,则必须首先将线条存储在数组或其他结构中,然后在
drawRect:
内迭代该结构并绘制每个项目。或者,使每行本身成为自定义视图(或图层)并将其添加为子视图。
Your view has no graphics context outside of
drawRect:
. You cannot make arbitrary drawing calls outside ofdrawRect:
and have them persist.If you need to dynamically add lines etc. to a view, you must first store the lines in an array or other structure, and then iterate that structure inside
drawRect:
and draw each item.Alternatively, make each line a custom view itself (or a layer) and add it as a subview.