ios 如何绘制 UIBezierPaths
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6434925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to draw UIBezierPaths
提问by Lordof Theflies
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.
我有一个 UIBezierPath,我想将它传递给某种方法来绘制它。或者简单地从创建它的方法中绘制它。
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?
如何调用该函数或方法,从另一种方法是什么?
回答by Deepak Danduprolu
drawRect:
is something that is invoked automatically when you message setNeedsDisplay
or setNeedsDisplayInRect:
on a view. You never call drawRect:
directly.
drawRect:
是什么,被调用时,你会自动消息setNeedsDisplay
或setNeedsDisplayInRect:
一个视图。你从不drawRect:
直接打电话。
However you are right in saying that all drawing operations are done within the drawRect:
method. Typical implementation would be,
但是,您说的所有绘图操作都在该drawRect:
方法内完成是正确的。典型的实现是,
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
/* Do your drawing on `context` */
}
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.
由于您使用UIBezierPath
的是s,因此您需要维护一组贝塞尔路径,您需要绘制这些路径,然后setNeedsDisplay
在发生变化时调用这些路径。
- (void)drawRect:(CGRect)rect {
for ( UIBezierPath * path in bezierPaths ) {
/* set stroke color and fill color for the path */
[path fill];
[path stroke];
}
}
where bezierPaths
is an array of UIBezierPath
s.
哪里bezierPaths
是UIBezierPath
s的数组。
回答by ZhangChn
First, save your path in an ivar
首先,将您的路径保存在 ivar 中
@interface SomeView {
UIBezierPath * bezierPath;
}
@property(nonatomic,retain) UIBezierPath * bezierPath;
...
@end
....
- (void)someMethod {
self.bezierPath = yourBezierPath;
[self setNeedsDisplayInRect:rectToRedraw];
}
in -drawRect:
在-drawRect:
- (void)drawRect:(CGRect)rect {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, 3.0);
CGContextSetLineCap(currentContext, kCGLineCapRound);
CGContextSetLineJoin(currentContext, kCGLineJoinRound);
CGContextBeginPath(currentContext);
CGContextAddPath(currentContext, bezierPath.CGPath);
CGContextDrawPath(currentContext, kCGPathStroke);
}
回答by cxa
When you need to custom your view, you can overwrite -drawRect:
on the subclass:
当您需要自定义视图时,您可以覆盖-drawRect:
子类:
- (void)drawRect:(CGRect)rect
{
// config your context
[bezierPath stroke];
}
Edit: directly using -stroke
make code more compact.
编辑:直接使用-stroke
使代码更紧凑。
回答by Dave DeLong
Drawing only happens inside a method called -drawRect:
(which is automatically called when a view is marked as needing display via setNeedsDisplay
). So a drawRect:withBezierPath:
method will never get invoked automatically. The only way it will execute is if you call it yourself.
绘图只发生在一个被调用的方法中-drawRect:
(当一个视图被标记为需要通过显示时自动调用setNeedsDisplay
)。所以一个drawRect:withBezierPath:
方法永远不会被自动调用。它将执行的唯一方法是您自己调用它。
Once you have a UIBezierPath
, however, it's very easy to draw it:
UIBezierPath
但是,一旦有了,就很容易绘制它:
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = ...; // get your bezier path, perhaps from an ivar?
[path stroke];
}
There's no need to futz around with Core Graphics if all you want to do is draw a path.
如果您只想绘制路径,则无需使用 Core Graphics。
回答by Rakesh
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];
您可以执行以下操作。只需定义一个 UIColor *setStroke; 在 .h 文件中,您需要在调用之前设置此 strokeColor 对象[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
- (void)drawRect:(CGRect)rect
{
[strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
for(UIBezierPath *_path in pathArray)
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth = currentSliderValue;
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
[pathArray addObject:myPath];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}