0

在此处输入图像描述

用户触摸并拖动图像顶部

用户触摸并拖动图像顶部

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

//UIView 圆形视图

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

}
4

1 回答 1

1

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

于 2013-12-05T00:51:11.760 回答