如何绘制 UIBezierPaths
这就是我想要做的:
我有一个 UIBezierPath,我想将它传递给某种方法来绘制它。或者简单地从创建它的方法中提取它。
我不确定如何指示应在哪个视图中绘制它。所有绘图方法都必须以
- (void)drawRect:(CGRect)rect { ...} ?
can I do
- (void)drawRect:(CGRect)rect withBezierPath:(UIBezierPath*) bezierPath { ... } ??
开头吗?如何从另一个方法调用此函数或方法?
Here's what I want to do:
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 { ...} ?
can I do
- (void)drawRect:(CGRect)rect withBezierPath:(UIBezierPath*) bezierPath { ... } ??
How do I call this function, or method, from another method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
drawRect:
是当您在视图上发送setNeedsDisplay
或setNeedsDisplayInRect:
消息时自动调用的东西。你永远不会直接调用drawRect:
。不过,您说得对,所有绘图操作都是在
drawRect:
方法中完成的。典型的实现是,由于您使用的是 UIBezierPath,因此您需要维护一个需要绘制的贝塞尔路径数组,然后在发生变化时调用 setNeedsDisplay。
其中,bezierPaths 是 UIBezierPath 的数组。
drawRect:
is something that is invoked automatically when you messagesetNeedsDisplay
orsetNeedsDisplayInRect:
on a view. You never calldrawRect:
directly.However you are right in saying that all drawing operations are done within the
drawRect:
method. Typical implementation would be,Since you are using
UIBezierPath
s, you will need to maintain an array of bezier paths that you will need to draw and then callsetNeedsDisplay
when something changes.where
bezierPaths
is an array ofUIBezierPath
s.首先,将路径保存在
-drawRect 的 ivar 中:
First, save your path in an ivar
in -drawRect:
当需要自定义视图时,可以在子类上覆盖
-drawRect:
:编辑:直接使用
-行程
使代码更加紧凑。When you need to custom your view, you can overwrite
-drawRect:
on the subclass:Edit: directly using
-stroke
make code more compact.绘图仅发生在名为
-drawRect:
的方法内(当通过setNeedsDisplay
将视图标记为需要显示时,会自动调用该方法)。因此,drawRect:withBezierPath: 方法永远不会被自动调用。它执行的唯一方法是您自己调用它。然而,一旦有了
UIBezierPath
,绘制它就非常容易了:如果您只想绘制一条路径,则无需使用 Core Graphics。
Drawing only happens inside a method called
-drawRect:
(which is automatically called when a view is marked as needing display viasetNeedsDisplay
). So adrawRect:withBezierPath:
method will never get invoked automatically. The only way it will execute is if you call it yourself.Once you have a
UIBezierPath
, however, it's very easy to draw it:There's no need to futz around with Core Graphics if all you want to do is draw a path.
你可以做类似以下的事情。只需定义一个 UIColor *setStroke;在 .h 文件中,您需要在调用
[myPathStrokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 之前设置此描边颜色对象;
you can do something like following. just define a UIColor *setStroke; in .h file and you need to set this strokeColor object before your you call
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];