绘图后如何将圆点分开

时间:2013-09-13 02:12:03

标签: ios touch core-graphics

enter image description here

user touch and drag on top of image

用户触摸并拖动图像顶部

我有这个代码会在绘制后给我一个圆圈,但我如何将它们分开?

// UIView CircleView

- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
    NSLog(@"started");
    // Initialize a new path for the user gesture
    self.path = [UIBezierPath bezierPath];
    self.path.lineWidth = 4.0f;

        self.whiteCircleArray = [[NSMutableArray alloc]init];

    UITouch *touch = [touches anyObject];
    [self.path moveToPoint:[touch locationInView:self]];
    self.touchedEnded = FALSE;

}

- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
    // Add new points to the path
    NSLog(@"Moved");

    UITouch *touch = [touches anyObject];
    //NSLog(@"[touch locationInView:self] %@",NSStringFromCGPoint([touch locationInView:self]));
    [self.whiteCircleArray addObject: NSStringFromCGPoint([touch locationInView:self])];
    [self.path addLineToPoint:[touch locationInView:self]];


    //double startAngle = RADIANS(0);
    //double endAngle = RADIANS(360);

//    if ([touch locationInView:self].x <=200 )
//    {
//        [self.path addArcWithCenter:[touch locationInView:self] radius:LARGE_RADIUS startAngle:startAngle endAngle:endAngle clockwise:NO];
//    }
//    else if ([touch locationInView:self].x >=120 )
//    {
//        [self.path addArcWithCenter:[touch locationInView:self] radius:SMALL_RADIUS startAngle:startAngle endAngle:endAngle clockwise:NO];
//    }

    //[self.path addArcWithCenter:[touch locationInView:self] radius:BIG_RADIUS startAngle:startAngle endAngle:endAngle clockwise:NO];
    //[self.path addArcWithCenter:[touch locationInView:self] radius:MIDDIUM_RADIUS startAngle:startAngle endAngle:endAngle clockwise:NO];

    [self setNeedsDisplay];
}


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"ended");
    UITouch *touch = [touches anyObject];
    //[self.path addLineToPoint:[touch locationInView:self]];
    self.locationOfTouch =[touch locationInView:self];
    self.touchedEnded = TRUE;

    //NSLog(@"white circle pos %@",self.whiteCircleArray);

    for(NSString *p in self.whiteCircleArray)
    {

        CGPoint point = CGPointFromString(p);
        NSLog(@"X=%0.1f Y=%0.1f",point.x,point.x);
    }

    [self setNeedsDisplay];
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
        NSLog(@"cancelled");
    [self touchesEnded:touches withEvent:event];
}

-(void)drawTouchCircle
{
    CGContextRef ctx= UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGContextSetLineWidth(ctx,1);

    CGContextSetRGBFillColor(ctx, 55.0/255, 255.0/255.0, 5.0/255.0, 1.0);
    CGContextSetRGBStrokeColor(ctx,0.0/255,0.0/255,0.0/255,1.0);

    for(NSString *p in self.whiteCircleArray)
    {
        CGPoint point = CGPointFromString(p);
        NSLog(@"X=%0.1f Y=%0.1f",point.x,point.x);
         CGContextAddArc(ctx, point.x, point.y, LARGE_RADIUS, 0, M_PI*2, YES);
        CGContextFillPath(ctx);
        CGContextStrokePath(ctx);
    }
}

- (void) drawRect:(CGRect)rect
{
    // Draw the path
    NSLog(@"drawRect");
    if (self.touchedEnded)
    {
         [self drawTouchCircle];
    }
    else
    {
    UIColor *strokeColor = [UIColor redColor];
    [strokeColor setStroke];
    [self.path stroke];
    }
   // [self.path  fill];
    //[self.path  closePath];

}

1 个答案:

答案 0 :(得分:1)

在算法中进行距离检查...... CGPoint diff = {p2.x - p1.x,p2.y - p1.y}如果差值大于,则添加一个新点。