如何在ios中获取所有坐标点(X和Y)
我正在编写一个应用程序,它允许用户在视图中绘制他们想要的任何东西。当他们绘图时,我使用Json将坐标值同时发送到web,并在web中绘制相同的图表。当我慢慢画的时候,我会得到所有的坐标值,就像这样如何在ios中获取所有坐标点(X和Y),ios,xcode,draw,uibezierpath,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",
{"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;
}
如何在快速绘制时找到所有坐标值?系统以一定间隔发送触摸事件。如果你走得慢,你会得到更多,但走得快,你会得到更少。如果你行动迅速,你就得不到更多。但你可能也不需要更多。你只需要在点之间画一条线,不管它们之间的距离是小是大。苹果产品以一定的间隔发送触摸事件。因此,如果你画/写任何东西都很慢,你就有可能得到所有的坐标值,如果你画/写得很快,你就会少画一些坐标值。并在web中应用您使用过的相同功能。希望现在可以解决此问题。可能是您的代码降低了触摸处理程序的速度,因此丢失了事件。首先在
touchesend
中删除jsonData
的创建,看看这是否会改善情况。@特洛伊木马甚至我尝试了touchesend之外的json数据,但得到了相同的结果。并且我NSLog字符串值rect_xy(即使在该坐标上)丢失。即使在添加称为“RECTARRY”的数组之前,用于字符串“rect_xy”的NSLog也丢失。是否有任何方法可以在特定间隔控制系统触摸事件?否。您无法更改系统发送触摸事件的间隔。但你为什么需要更多?也许你的问题还有另一个解决方案。实际上,该应用程序将向用户显示MQTT服务器速度。所以,用户绘制的任何东西都应该显示在网页上,如果我得到了所有的坐标,那么它就会工作。如果坐标值遗漏了仅在网络上显示的虚线。好吧,和你在iOS上做的一样,你必须在网络上做:-在点之间画一条线,而不仅仅是画点。