2012-07-21 68 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