更改CPTPlotSpaceAnnotation的图层形状?

时间:2012-07-21 01:53:43

标签: core-animation core-plot

我想在我的图表中添加一个用户可以点击并拖动的小部件。我已经设法使用CPTPlotSpaceAnnonation(因为我也希望它也可以滚动数据)和CPTBorderedLayer。基本功能有效,现在我想改进小部件的外观。到目前为止,它只是一个绿色圆圈。

以下代码生成下面的圆圈,注意阴影如何剪裁到图层的矩形,边框跟随图层而不是圆圈,

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

1 个答案:

答案 0 :(得分:0)

不要设置图层的边框路径。相反,只需将cornerRadius设置为帧宽度的一半即可。

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