如何绘制UIBezierPaths [英] how to draw UIBezierPaths
问题描述
这就是我想要做的事情:
Here's what I want to do:
我有一个UIBezierPath,我想把它传递给某个方法来绘制它。或者只是从创建它的方法中绘制它。
I have a UIBezierPath and I want to pass it to some method for it to be drawn. Or simply draw it from the method in which it is created.
我不知道如何指出应该绘制哪个视图。所有绘图方法都必须以
I'm not sure how to indicate which view it should be drawn in. Do all methods for drawing have to start with
- (void)drawRect:(CGRect)rect { ...} ?
我可以吗
- (void)drawRect:(CGRect)rect withBezierPath:(UIBezierPath*) bezierPath { ... } ??
如何从其他方法调用此函数或方法?
How do I call this function, or method, from another method?
推荐答案
drawRect:
是在您发送消息 setNeedsDisplay <时自动调用的内容视图上的/ code>或
setNeedsDisplayInRect:
。你永远不会直接打电话给 drawRect:
。
drawRect:
is something that is invoked automatically when you message setNeedsDisplay
or setNeedsDisplayInRect:
on a view. You never call drawRect:
directly.
然而你说的是所有的绘图操作都是在 drawRect:
方法。典型的实现方式是,
However you are right in saying that all drawing operations are done within the drawRect:
method. Typical implementation would be,
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
/* Do your drawing on `context` */
}
由于您使用的是 UIBezierPath
,因此您需要维护一个需要绘制的bezier路径数组,然后调用 setNeedsDisplay
当某些内容发生变化时。
Since you are using UIBezierPath
s, you will need to maintain an array of bezier paths that you will need to draw and then call setNeedsDisplay
when something changes.
- (void)drawRect:(CGRect)rect {
for ( UIBezierPath * path in bezierPaths ) {
/* set stroke color and fill color for the path */
[path fill];
[path stroke];
}
}
其中 bezierPaths
是一个 UIBezierPath
的数组。
这篇关于如何绘制UIBezierPaths的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!