2012-09-12 23 views
1

我一直在爲此工作了幾天,並且非常難過。我正在試圖在覈心圖中繪製一個圖表,允許用戶按下數據點並獲取座標。我做了一個示例應用程序,它只是生成一個線性圖。我的問題是我無法使觸摸功能正常工作。我參考了核心網站上的許多情節,但我無法在我的應用中使用它。這是我所做的;核心劇情1.0我的觸摸註釋將無法正常工作

在我的頭文件中,我做了一個屬性CPTPlotSpaceAnnotation *註解。我也用過,界面ViewController:UIViewController。

在我的實現中,我合成了註釋。在我的ViewDidAppear方法中,我用CGPointMake製作了一個數據數組。

NSMutableArray *linearstuff = [NSMutableArray array]; 

for (int i = 0; i < 100; i ++) 
{ 
    float y = (b * (l)) + c; 
    [linearstuff addObject:[NSValue valueWithCGPoint:CGPointMake(l, y)]]; 
    l = l + inc; 
} 

self.data = linearstuff; 
[self initPlot]; 

我的方法initPlot配置圖形,並有幾個配置方法。所以在我調用configurePlots的方法中,我分配並初始化一個名爲linearGraph的CPTScatterPlot。我在該方法中也使用了linearGraph.plotSymbolMarginForHitDetection。然後對於plotSymbolWasSelectedAtRecordIndex,我做了以下操作。

- (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]; 
} 

我試圖按照我在覈心網站上看到的內容。我拿了我的數組,並得到了索引。我將這些值放在一個NSNumber中爲註釋創建一個字符串。但是,它並沒有出現在圖表上。有人知道我做錯了嗎?任何有識之士將不勝感激。我已經閱讀了許多關於SO的核心情節問題,但找不到有助於我的情況的東西。先謝謝你。

+0

您是否驗證過'anchorPoint'和標籤文本是您所期望的?我假設情節區域有一個非白色填充。 :-) –

+0

@Eric Skroch,是的,情節沒有主題,所以我的藍色背景來臨了。至於驗證,我已經嘗試過NSLog的東西,但他們沒有出現。到目前爲止,我在代碼中使用的NSLog「touch」從未出現過。你認爲這意味着它永遠不會被稱爲?我在你的一個例子中做了NSLog方法,它也沒有出現。再次感謝你的幫助! – Douglas

+0

你應該看到日誌消息 - 幾個例子可以做到這一點,它工作正常。你用什麼'plotSymbolMarginForHitDetection'?嘗試一個相當大的價值,只是爲了看看你能否得到它的工作。 –

回答

1

對於任何關注此事的人。我首先要感謝Eric Skroch的所有幫助,再次!!!!但我終於發現我做錯了什麼。我已經讓viewController成爲劇情委託人,但是,當我製作劇情時,我沒有這麼說。我添加了linearGraph.delegate = self;現在調用plotSymbol ...方法!謝謝@Eric Skroch。

+0

我也必須改變我的plotSymbolWas ....方法。查看我的原始帖子,查看更新的代碼。 – Douglas

+0

感謝一些我從你的帖子得到的想法....不錯的工作 – Spynet

+0

@Spynet,我很高興它幫助你了! – Douglas