【问题标题】:Core Plot-How to Convert Touch-Sensed Point to Point in PlotSpace?Core Plot-如何在PlotSpace中将触摸感应到的点转换为点?
【发布时间】: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


    【解决方案1】:

    使用绘图空间(作为参数传递给此方法)在数据坐标和绘图区域视图坐标之间进行转换。

    使用以下方法之一从event 中提取触摸坐标并将其转换为数据坐标。

    -(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]; [空间 plotPoint:plotPoint forPlotAreaViewPoint:point];,但没有为 plotPoint:plotAreaViewPointForEvent 声明@interface
    • 您使用的是什么版本的 Core Plot? “事件”方法从 1.1 版开始就存在(1.5.1 是最新版本)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多