Iphone 绘制中心不重叠的直线

Iphone 绘制中心不重叠的直线,iphone,ios,drawrect,Iphone,Ios,Drawrect,我试着加入队伍时遇到了问题。这是一张照片: 我喜欢它看起来像这样: 我的代码是: - (void) drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); const CGFloat *components = CGColorGetComponents(self.lineColor.CGColor); CGFlo

我试着加入队伍时遇到了问题。这是一张照片:

我喜欢它看起来像这样:

我的代码是:

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

    const CGFloat *components = CGColorGetComponents(self.lineColor.CGColor);

    CGFloat red;
    CGFloat green;
    CGFloat blue;
    CGFloat alpha;

    if(CGColorGetNumberOfComponents(self.lineColor.CGColor) == 2)
    {
        red   = 1;
        green = 1;
        blue  = 1;
        alpha = 1;
    }
    else
    {
        red   = components[0];
        green = components[1];
        blue  = components[2];
        alpha = components[3];
        if (alpha <= 0) alpha = 1;
    }


    // set the stroke color and width
    CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
    CGContextSetLineWidth(context, 2.0);

    if (self.points.count >0) {


    BezierPoint *firstPoint = [self.points objectAtIndex:0];

    CGContextMoveToPoint(context, firstPoint.center.x, firstPoint.center.y);

    int index = 0;
    for (BezierPoint *point in self.points ) {


        if(index == 0){
            index++;
            continue;
        }

        CGContextAddLineToPoint(context, point.center.x, point.center.y);

    }

    CGContextAddLineToPoint(context, firstPoint.center.x, firstPoint.center.y);
    }


    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.0);
    CGContextDrawPath(context, kCGPathFillStroke);

    }
-(void)drawRect:(CGRect)rect{
[超级drawRect:rect];
CGContextRef context=UIGraphicsGetCurrentContext();
const CGFloat*components=CGColorGetComponents(self.lineColor.CGColor);
cg-浮红;
绿色;
CGFloat蓝;
CGAα;
if(CGColorGetNumberOfComponents(self.lineColor.CGColor)==2)
{
红色=1;
绿色=1;
蓝色=1;
α=1;
}
其他的
{
红色=组件[0];
绿色=组件[1];
蓝色=组件[2];
α=成分[3];
if(alpha 0){
BezierPoint*firstPoint=[self.points对象索引:0];
CGContextMoveToPoint(上下文,firstPoint.center.x,firstPoint.center.y);
int指数=0;
用于(BezierPoint*点在self.points中){
如果(索引==0){
索引++;
继续;
}
CGContextAddLineToPoint(上下文,point.center.x,point.center.y);
}
CGContextAddLineToPoint(上下文,firstPoint.center.x,firstPoint.center.y);
}
CGContextSetRGBFillColor(上下文,0.0,0.0,0.0,0.0);
CGContextDrawPath(上下文,kCGPathFillStroke);
}
我的问题是,对于你添加的每一个点,线都会重叠,我希望在我停留的时候,我为几何图形添加点,球蛋白不会重叠


如果有人能帮助我,我将非常感谢!!

纠正您的问题的简单方法是首先在两点之间只画一条线段,然后查看结果。然后您可以形成循环。我猜您没有指定完美的坐标点,或者您的循环必须包括:

CGContextAddLineToPoint(上下文,point.center.x,point.center.y)

然后呢,, CGContextMoveToPoint(上下文,point.center.x,point.center.y)

这表明第一个绘制线并移动到下一个点。
希望这会有所帮助。

我会将确定线条绘制顺序的逻辑移动到视图控制器,并使用协议从代理访问绘制视图所需的数据。视图不应拥有其数据。例如,在.h中,请尝试以下操作:

#import <UIKit/UIKit.h>

@protocol CowDrawViewDataSource
-(NSArray *) pointsToDraw;
@end

@interface CowDrawView : UIView
@property (nonatomic , weak) id <CowDrawViewDataSource> dataSource;
@end
方法,以这种方式按绘制顺序发送点阵列。例如,通过查找点—顶部/左侧点,然后是顶部/右侧点,然后是底部/右侧点,底部/左侧点。(尽管这种方法可能无法很好地处理不规则多边形和相邻但不是多边形的形状。)

在确定如何按所需顺序获取数组后,可以通过向其委托(如)发送消息在drawRect中获取该数组

NSArray *points = [self.dataSource pointToDraw];

在drawRect中只需很少的重新工作,它就是自己。

谢谢你的回复。我在touchesBegind方法中添加了点:。这个想法是,当我触摸屏幕时,会用我在图片中显示的点来武装一个区域。我在一个数组中调用点。所以你是通过触摸点来创建数组的。你尝试过上面的解决方案吗是的,我在视图中添加了一个带有点的数组。是的,在我的例子中,它工作得很好!
NSArray *points = [self.dataSource pointToDraw];