如何获取ios中的所有坐标点(X和Y)

时间:2015-05-20 05:40:39

标签: ios xcode draw uibezierpath

我正在编写一个应用程序,允许用户在视图中绘制他们想要的内容。在绘制时,我使用Json将坐标值同时发送到Web,并在Web中绘制相同的图表。当我慢慢绘制时,我得到所有坐标值。

{"XY":["92,240","94,240","96,240","97,240","98,240","99,240","100,240","102,240","103,240","104,240","104,240","105,240","106,240","107,240","108,240","108,240","110,240","110,240","112,240","112,240","114,240","115,240","116,240","117,240","118,240","120,240","120,240","120,240","122,240","122,240","124,240","124,240","126,240"]}

但是当我快速绘制时,我得到了所需的绘图,但缺少很多坐标值。

{"XY":["96,320","117,302","170,262","252,208"]}

以下代码用于实现此目的。

@implementation PJRSignatureView
{
    UIBezierPath *beizerPath;
    UIImage *incrImage;
    CGPoint points[10];
    uint control;
}
- (void)drawRect:(CGRect)rect
{
    [incrImage drawInRect:rect];
    [beizerPath stroke];

    // Set initial color for drawing

    UIColor *fillColor = INITIAL_COLOR;
    [fillColor setFill];
    UIColor *strokeColor = INITIAL_COLOR;
    [strokeColor setStroke];
    [beizerPath stroke];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([lblSignature superview]){
        [lblSignature removeFromSuperview];
    }
    rectArray = [[NSMutableArray alloc] init];
    control = 0;
    UITouch *touch = [touches anyObject];
    points[0] = [touch locationInView:self];

    CGPoint startPoint = points[0];
    CGPoint endPoint   = CGPointMake(startPoint.x + 1.5, startPoint.y
                              + 2);



   [beizerPath moveToPoint:startPoint];
        NSLog(@"myPoint = %@", [NSValue valueWithCGPoint:endPoint]);
        NSLog(@"beizerPath    :%@",beizerPath);
    [beizerPath addLineToPoint:endPoint];
     NSLog(@"beizerPath end:%@",beizerPath);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    control++;
    points[control] = touchPoint;

    if (control == 4)
    {
        points[3] = CGPointMake((points[2].x + points[4].x)/2.0, (points[2].y + points[4].y)/2.0);

        [beizerPath moveToPoint:points[0]];
        [beizerPath addCurveToPoint:points[3] controlPoint1:points[1] controlPoint2:points[2]];

        [self setNeedsDisplay];

        points[0] = points[3];
        points[1] = points[4];
        control = 1;
    }
       NSLog(@"beizerPathmove:%@",beizerPath);
    NSString *rect_xy = [NSString stringWithFormat:@"%.f,%.f",touchPoint.x,touchPoint.y];

    [rectArray addObject:rect_xy];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self drawBitmapImage];
    [self setNeedsDisplay];
    [beizerPath removeAllPoints];

    NSMutableDictionary *rectDict = [[NSMutableDictionary alloc]init];
    [rectDict setValue:rectArray forKey:@"XY"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:rectDict options:0 error:nil];

    // Checking the format
    NSLog(@"%@",[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
    control = 0;
}

即使我快速绘制,如何找到所有坐标值?

2 个答案:

答案 0 :(得分:0)

系统以特定间隔发送触摸事件。如果你移动得慢,你会得到更多,但是你走得越快,你就越少。如果你快速行动,你就无法获得更多。但你也可能不需要更多。无论它们之间的距离是小还是大,你都必须在点之间画线。

答案 1 :(得分:0)

Apple产品会以一定间隔发送触摸事件。所以,如果你慢慢地绘制/写入任何东西以获得所有的坐标值,如果你快速绘制/写入,你将减少其中的一些。并在您使用的Web中应用相同类型的功能。希望现在能解决这个问题。