【发布时间】:2014-08-09 22:53:03
【问题描述】:
我是核心情节的新手。我正在尝试计算与以下代码中感应到触摸的坐标最近的数据点:
-(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;
}
但是,我刚刚意识到,此方法返回的触摸坐标不是在与图表上绘制的坐标相同的系统中测量的。例如,触摸图形上的坐标 (2,60) 将返回像 (188,150) 这样的坐标,这是很遥远的。有没有办法在图形坐标系中找到say (188,150) 的等价物?
【问题讨论】:
标签: ios coordinates core-plot