如何绘制 UIBezierPaths

发布于 2024-11-16 17:00:13 字数 335 浏览 4 评论 0原文

这就是我想要做的:

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不再让梦枯萎 2024-11-23 17:00:13

drawRect: 是当您在视图上发送 setNeedsDisplaysetNeedsDisplayInRect: 消息时自动调用的东西。你永远不会直接调用drawRect:

不过,您说得对,所有绘图操作都是在 drawRect: 方法中完成的。典型的实现是,

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    /* Do your drawing on `context` */
}

由于您使用的是 UIBezierPath,因此您需要维护一个需要绘制的贝塞尔路径数组,然后在发生变化时调用 setNeedsDisplay。

- (void)drawRect:(CGRect)rect {    
    for ( UIBezierPath * path in bezierPaths ) {
        /* set stroke color and fill color for the path */
        [path fill];
        [path stroke];
    }
}

其中,bezierPaths 是 UIBezierPath 的数组。

drawRect: is something that is invoked automatically when you message setNeedsDisplay or setNeedsDisplayInRect: on a view. You never call drawRect: directly.

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` */
}

Since you are using UIBezierPaths, 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];
    }
}

where bezierPaths is an array of UIBezierPaths.

夜空下最亮的亮点 2024-11-23 17:00:13

首先,将路径保存在

@interface SomeView {
  UIBezierPath * bezierPath;
}
@property(nonatomic,retain) UIBezierPath * bezierPath;
...
@end
....
- (void)someMethod {
     self.bezierPath = yourBezierPath;
     [self setNeedsDisplayInRect:rectToRedraw];
}

-drawRect 的 ivar 中:

- (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);
}

First, save your path in an ivar

@interface SomeView {
  UIBezierPath * bezierPath;
}
@property(nonatomic,retain) UIBezierPath * bezierPath;
...
@end
....
- (void)someMethod {
     self.bezierPath = yourBezierPath;
     [self setNeedsDisplayInRect:rectToRedraw];
}

in -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);
}
微凉徒眸意 2024-11-23 17:00:13

当需要自定义视图时,可以在子类上覆盖-drawRect:

- (void)drawRect:(CGRect)rect
{
  // config your context
  [bezierPath stroke];
}

编辑:直接使用-行程使代码更加紧凑。

When you need to custom your view, you can overwrite -drawRect: on the subclass:

- (void)drawRect:(CGRect)rect
{
  // config your context
  [bezierPath stroke];
}

Edit: directly using -stroke make code more compact.

贪恋 2024-11-23 17:00:13

绘图仅发生在名为 -drawRect: 的方法内(当通过 setNeedsDisplay 将视图标记为需要显示时,会自动调用该方法)。因此,drawRect:withBezierPath: 方法永远不会被自动调用。它执行的唯一方法是您自己调用它。

然而,一旦有了 UIBezierPath,绘制它就非常容易了:

- (void)drawRect:(CGRect)rect {
  UIBezierPath *path = ...; // get your bezier path, perhaps from an ivar?
  [path stroke];
}

如果您只想绘制一条路径,则无需使用 Core Graphics。

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.

Once you have a UIBezierPath, however, it's very easy to draw it:

- (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.

奶气 2024-11-23 17:00:13

你可以做类似以下的事情。只需定义一个 UIColor *setStroke;在 .h 文件中,您需要在调用 [myPathStrokeWithBlendMode: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];

    }

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];

 - (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];

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文