Ios 核心绘图如何在绘图空间中将触摸感应点转换为点?
我不熟悉核心情节。我正试图计算距离以下代码中感应到触摸的坐标最近的数据点:Ios 核心绘图如何在绘图空间中将触摸感应点转换为点?,ios,coordinates,core-plot,Ios,Coordinates,Core Plot,我不熟悉核心情节。我正试图计算距离以下代码中感应到触摸的坐标最近的数据点: -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point { NSLog(@"touch sensed"); NSLog(@"points: %f, %f",point.x,point.y); [self.distances removeAl
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point
{
NSLog(@"touch sensed");
NSLog(@"points: %f, %f",point.x,point.y);
[self.distances removeAllObjects];
CPTGraph *graph = self.hostView.hostedGraph;
if (symbolTextAnnotation) {
[graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
symbolTextAnnotation = nil;
}
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0;
hitAnnotationTextStyle.fontName = @"Helvetica-Bold";
for(int i=0;i<[self.allX count];i++){
//NSLog(@"-->all points: %@, %@", [self.allX objectAtIndex:i],[self.allY objectAtIndex:i]);
NSLog(@"-->all points: %f, %f", [[self.allX objectAtIndex:i] floatValue],[[self.allY objectAtIndex:i] floatValue]);
CGFloat xDist = (point.x - [[self.allX objectAtIndex:i] floatValue]);
CGFloat yDist = (point.y - [[self.allY objectAtIndex:i] floatValue]);
NSLog(@"-->all distances: %f, %f", xDist,yDist);
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));
NSNumber *distanceNum = [NSNumber numberWithFloat:distance];
NSLog(@"-->total distance: %@", distanceNum);
[self.distances addObject:distanceNum];
}
for(int i=0;i<[self.distances count];i++){
NSLog(@"calculated distances: %@",[self.distances objectAtIndex:i]);
}
NSNumber *minDistance = [self.distances valueForKeyPath:@"@min.doubleValue"];
NSLog(@"min distance: %@", minDistance);
int index=[self.distances indexOfObject:minDistance];
NSLog(@"cloest point: %@,%@",[self.allX objectAtIndex:index],[self.allY objectAtIndex:index]);
NSArray *anchorPoint = @[[self.allX objectAtIndex:index], [self.allY objectAtIndex:index] ];
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:@"LABEL" style:hitAnnotationTextStyle] ;
symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
symbolTextAnnotation.contentLayer = textLayer;
symbolTextAnnotation.displacement = CGPointMake(0.0, 20.0);
[graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];
return 1;
}
-(BOOL)绘图空间:(CPTPlotSpace*)空间应与指针设备DownEvent:(id)事件在点:(CGPoint)点
{
NSLog(“触摸感应”);
NSLog(@“点:%f,%f”,点x,点y);
[self.removeAllObjects];
CPTGraph*graph=self.hostView.hostdGraph;
if(symbolTextAnnotation){
[graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
symbolTextAnnotation=nil;
}
CPTMutableTextStyle*hitAnnotationTextStyle=[CPTMutableTextStyle];
hitAnnotationTextStyle.color=[CPTColor whiteColor];
hitAnnotationTextStyle.fontSize=16.0;
hitAnnotationTextStyle.fontName=@“Helvetica Bold”;
对于(int i=0;i使用绘图空间(作为此方法的参数传递)在数据坐标和绘图区域视图坐标之间进行转换
使用以下方法之一从事件中提取触摸坐标
,并将其转换为数据坐标
-(void)plotPoint:(NSDecimal *)plotPoint numberOfCoordinates:(NSUInteger)count forEvent:(CPTNativeEvent *)event;
-(void)doublePrecisionPlotPoint:(double *)plotPoint numberOfCoordinates:(NSUInteger)count forEvent:(CPTNativeEvent *)event;
或者,使用-plotAreaViewPointForEvent:
方法从事件中提取触点的视图坐标,并使用以下方法之一将数据坐标转换为视图坐标,并计算该坐标系中的距离:
-(CGPoint)plotAreaViewPointForPlotPoint:(NSDecimal *)plotPoint numberOfCoordinates:(NSUInteger)count;
-(CGPoint)plotAreaViewPointForDoublePrecisionPlotPoint:(double *)plotPoint numberOfCoordinates:(NSUInteger)count;
我使用了以下代码,与您所说的类似:NSDecimal plotPoint[2];[space plotPoint:plotPoint FORPLOTAREA VIEW:point];,但没有为plotPoint:PLOTAREA VIEWPOINT声明@接口,即使您使用的是哪一版本的核心绘图?自版本1.1(1.5.1是最新版本)起,“事件”方法就一直存在。