Core Plot注释触摸事件

时间:2013-04-03 12:34:49

标签: ios objective-c core-plot

我在我的应用程序中使用Core Plot。 我需要让绘图的CPTAnnotation个对象响应用户的触摸事件。 这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

CPPlotSpaceDelegate协议用于触摸事件设置委托

您需要在视图控制器中调用以下方法

-(BOOL)plotSpace:(CPPlotSpace *)space shouldHandlePointingDeviceUpEvent:(id)event atPoint:(CGPoint)point;

-(BOOL)plotSpace:(CPPlotSpace *)space shouldHandlePointingDeviceCancelledEvent:(id)event;

-(BOOL)plotSpace:(CPPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point;

-(BOOL)plotSpace:(CPPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)point;

对于注释,您需要使用...

- (void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
NSLog(@"touch");
CPTGraph *graph = self.hostView.hostedGraph;

if (_annotation)
{
    [graph.plotAreaFrame.plotArea removeAnnotation:_annotation];
    _annotation = nil;
}

CPTMutableTextStyle *annotationTextStyle = [CPTMutableTextStyle textStyle];
annotationTextStyle.color = [CPTColor whiteColor];
annotationTextStyle.fontSize = 16.0f;
annotationTextStyle.fontName = @"Helvetica-Bold";

NSValue *value = [self.data objectAtIndex:index];
CGPoint point = [value CGPointValue];
NSString *number1 = [NSString stringWithFormat:@"%.2f", point.x];
NSString *number2 = [NSString stringWithFormat:@"%.2f", point.y];

NSLog(@"x and y are, %.2f, %.2f", point.x, point.y);
NSLog(@"number1 and number2 are, %.2f, %.2f", point.x, point.y);

NSNumber *x = [NSNumber numberWithFloat:point.x];
NSNumber *y = [NSNumber numberWithFloat:point.y];

NSArray *anchorPoint = [NSArray arrayWithObjects: x, y, nil];

NSString *final = [number1 stringByAppendingString:number2];
NSLog(@"final is %@",final);


CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:final style:annotationTextStyle];
_annotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
_annotation.contentLayer = textLayer;
_annotation.displacement = CGPointMake(0.0f, 0.0f);
[graph.plotAreaFrame.plotArea addAnnotation:_annotation];
}

答案 1 :(得分:0)

CPTAnnotation 包含contentLayer属性,属性类型为 CPTLayer SPTLayer 确认接口 CPTResponder

您可以为内容图层创建自定义类,并覆盖 CPRResponder 中的方法:

(BOOL)  - pointingDeviceDownEvent:atPoint:
(BOOL)  - pointingDeviceUpEvent:atPoint:
(BOOL)  - pointingDeviceDraggedEvent:atPoint:
(BOOL)  - pointingDeviceCancelledEvent: