【问题标题】:Changing the layer shape for CPTPlotSpaceAnnotation?更改 CPTPlotSpaceAnnotation 的图层形状?
【发布时间】:2012-07-21 01:53:42
【问题描述】:

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

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

我怎样才能描边而不是图层?我想我可以通过使图层的框架更大来避免剪裁。是否可以将 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];

【问题讨论】:

  • 注解似乎是被描边的那个,它仍然是方形的。似乎注释也将其子视图裁剪到自己的范围内。

标签: core-animation core-plot


【解决方案1】:

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

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

【讨论】:

  • 我没想到,谢谢。也许没有阴影这是最好的解决方案。但是它会剪切阴影和框架,就像上面一样。
  • 我认为 CorePlot 无法做到这一点,因为它需要一个 CAShapeLayer,而 CorePlot 注释代码使用 CALayer。我决定尝试实现与 CAShapeLayer 一起使用的自定义注释。
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 2014-01-09
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多