2012-07-21 57 views
1

我想將小工具添加到我的圖形,用戶可以點擊並拖動。我已經成功地做到這一點使用CPTPlotSpaceAnnonation(因爲我也希望它也與數據滾動)和CPTBorderedLayer。基本功能的作品,現在我想改善小部件的外觀。到目前爲止,這只是一個綠色的圓圈。更改爲CPTPlotSpaceAnnotation層形狀?

後續代碼生成圓下方,注意陰影是如何夾在層的正確,而且邊框遵循層不圓,

enter image description here

我怎樣才能中風圓而不是圖層?我想我可以通過使圖層的框架變大來避免裁剪。可以使用CAShapeLayer類和Core Plot註釋嗎?這將是繪製小部件的簡單方法。

// Add a circular annotation to graph with fill and shadow 
annotation = [[CPTPlotSpaceAnnotation alloc] 
        initWithPlotSpace:graph.defaultPlotSpace 
        anchorPlotPoint:anchorPoint]; 

// Choosing line styles and fill for the widget 
NSRect frame = NSMakeRect(0, 0, 50.0, 50.0); 
CPTBorderedLayer *borderedLayer = [[CPTBorderedLayer alloc] initWithFrame:frame]; 

// Circular shape with green fill 
CGPathRef outerPath = CGPathCreateWithEllipseInRect(frame, NULL); 
borderedLayer.outerBorderPath = outerPath; 
CPTFill *fill = [CPTFill fillWithColor:[CPTColor greenColor]]; 
borderedLayer.fill = fill; 

// Basic shadow 
CPTMutableShadow *shadow = [CPTMutableShadow shadow]; 
shadow.shadowOffset = CGSizeMake(0.0, 0.0); 
shadow.shadowBlurRadius = 6.0; 
shadow.shadowColor = [[CPTColor blackColor] colorWithAlphaComponent:1.0]; 
borderedLayer.shadow = shadow; 

// Add to graph 
annotation.contentLayer = borderedLayer; 
annotation.contentLayer.opacity = 1.0; 
[graph addAnnotation:annotation]; 
+0

它看起來像註釋是被撫摸,它仍然是正方形。它看起來好像是註解將其子視圖限制在自己的範圍內。 – 2012-07-21 08:22:13

回答

0

不要設置圖層的邊框路徑。相反,只需將cornerRadius設置爲框架寬度的一半即可。

borderedLayer.cornerRadius = frame.size.width * 0.5; 
+0

我沒想到,謝謝。也許沒有影子這是最好的解決方案。但它的剪影和框架,就像上面那樣。 – 2012-07-23 12:35:53

+0

我不認爲這可以用CorePlot完成,因爲它需要一個CAShapeLayer,其中作爲CorePlot註釋代碼使用的CALayer。我決定嘗試並實施與CAShapeLayer一起使用的自定義註釋。 – 2012-07-29 03:50:42