改变用于CPTPlotSpaceAnnotation层形状?(Changing the layer

2019-10-17 01:21发布

我想一个小工具添加到我的图形,用户可以点击并拖动。 我已经成功地做到这一点使用CPTPlotSpaceAnnonation(因为我也希望它也与数据滚动)和CPTBorderedLayer。 基本功能的作品,现在我想改善小部件的外观。 到目前为止,这只是一个绿色的圆圈。

后续的代码生成圆下方,注意阴影是如何夹在层的正确,而且边框遵循层不圆,

哪能行程圈,而不是层? 我想我可以通过使层大的帧避免限幅。 可以使用CAShapeLayer类核心情节注解? 这将是绘制控件的一个简单的方法。

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

Answer 1:

不要设置层的边缘路径。 相反,只需设置cornerRadius到帧的宽度的一半。

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


文章来源: Changing the layer shape for CPTPlotSpaceAnnotation?