如何用CGPath绘制多边形

如何用CGPath绘制多边形

本文介绍了如何用CGPath绘制多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读文档,但是我并不是很清楚如何使用CGPath绘制多边形。我需要做的就是在这样的事情周围绘制CGPath:

I have been reading thru the documentation however it is not immediatly clear to me how to draw a polygon using CGPath. All I need to do is to draw CGPath around something like this:

__
\  \
 \  \
  \__\

任何人都可以提供一个片段怎么做?

Could anyone please provide an snippet on how to do this?

另外我假设CGPathContainsPoint将帮助我确定一个点是否在这样的路径中?或者路径是否必须是一个实体图纸

Additionally I assume CGPathContainsPoint will help me determine if a point is inside such path?, or does the path have to be a solid drawing

另外我如何移动cgpath?这就像在cgrect中更改像原点一样容易吗?

Also how can i move the cgpath around? Is this as easy as changing something like the origin just like in cgrect?

谢谢。

-Oscar

推荐答案

你应该这样做:

- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);

        // Draw them with a 2.0 stroke width so they are a bit more visible.
        CGContextSetLineWidth(context, 2.0);

        for(int idx = 0; idx < self.points.count; idx++)
        {

            point = [self.points objectAtIndex:idx];//Edited
            if(idx == 0)
            {
                // move to the first point
                CGContextMoveToPoint(context, point.x, point.y);
            }
            else
            {
                CGContextAddLineToPoint(context, point.x, point.y);
            }
        }

        CGContextStrokePath(context);
}

请注意,这里是要绘制多边形的点数组对于。所以它应该是圆形路径,如:你正在绘制点的三角形(x1,x2,x3)然后你应该传入数组(x1, x2,x3,x1)

Note here, the points is the array of points you want to draw the polygon for. So it should be circular path like: You are drawing a triangle of points (x1, x2, x3) then you should pass into array (x1, x2, x3, x1).

希望这会有所帮助。

这篇关于如何用CGPath绘制多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:43