简体   繁体   English

正弦CAShapeLayer作为CALayer的面具

[英]Sine CAShapeLayer as a mask of CALayer

I'm trying to implement the next curious thing: I have a few dots and I'd like to link them with sine waves 我正在尝试实现下一个奇怪的事情:我有几个点,我想用正弦波连接它们

Next method returns the array of dots for making plot: 下一个方法返回用于制作绘图的点阵列:

- (NSArray *)plotPointsBetweenPoint:(CGPoint)pointA andPoint:(CGPoint)pointB {
  double H = fabs(pointB.y - pointA.y);
  double L = fabs(pointB.x - pointA.x);

  NSInteger granularity = 40;
  NSMutableArray *array = [NSMutableArray arrayWithCapacity:granularity + 1];
  for (int i = 0; i <= granularity; ++i) {
    CGFloat x = M_PI*(float)i/(granularity);
    CGFloat y = -cosf(x);
    CGFloat multiplier = pointA.y > pointB.y ? 1 : -1;
    CGPoint newPoint = CGPointMake(pointA.x + L*(float)i/granularity, pointA.y - multiplier*H*(1.0 + y)/2.0);
    [array addObject:[NSValue valueWithCGPoint:newPoint]];
  }
  return array;
}

After it I create the special UIBezierPath : 之后我创建了特殊的UIBezierPath

- (UIBezierPath *)sineWaveWithPoints:(NSArray *)points {
  UIBezierPath *path = [[UIBezierPath alloc] init];
  NSMutableArray *array = [NSMutableArray array];

  for (int i = 0; i < points.count - 1; ++i) {
    [array addObjectsFromArray:[self plotPointsBetweenPoint:[points[i] CGPointValue] andPoint:[points[i+1] CGPointValue]]];
  }
  [path moveToPoint:[array[0] CGPointValue]];
  for (NSValue *value in array) {
    CGPoint point = [value CGPointValue];
    [path addLineToPoint:point];
  }
  [path closePath];
  path.lineWidth = 2.0;
  [path stroke];
  return path;
}

Next I'm adding this path using CAShapeLayer: 接下来我使用CAShapeLayer添加此路径:

CAGradientLayer *gradientLayer = [self gradientLayer];
CAShapeLayer *maskLine = [CAShapeLayer layer];
maskLine.path = [self sineWaveWithPoints:pointsArray].CGPath;
maskLine.lineWidth = self.plotWidth;
gradientLayer.mask = maskLine;
[self.view.layer addSublayer:gradientLayer];

这就是我最终拥有的

So I'd like to have gradient sine wave between this points. 所以我想在这些点之间有渐变正弦波。 What exactly is wrong in my actions? 我的行为究竟出了什么问题?

Ideal: 理想: 在此输入图像描述

The program is behaving correctly - that is, it is doing exactly what you are telling it to do. 该程序运行正常 - 也就是说,它正在完成你告诉它要做的事情。 The problem is that what you are telling it to do (in your code) is not what you say you want (in your words and picture in your question). 问题在于你告诉它(在你的代码中)不是你想要的(在你的问题中用你的文字和图片)。

First thing you're doing wrong: you said closePath . 首先你做错了:你说closePath So it's closing the path! 所以它正在关闭这条道路! That means that, having drawn the wave, it connects the last point back to the first point with a straight line, giving the shape that you show. 这意味着,绘制波后,它将最后一个点连接到第一个点,用直线连接,给出你显示的形状。

Second thing you're doing wrong: you are failing to manipulate the shape layer. 第二件事你做错了:你没有操纵形状层。 You are making a vanilla CAShapeLayer and leaving it at its defaults. 你正在制作一个vanilla CAShapeLayer并将其保留为默认值。 Well, the default is that the fillColor is black (and no strokeColor ). 好吧,默认情况下fillColor是黑色的(并且没有strokeColor )。 You are not changing that. 你没有改变它。 So you are getting your (incorrect) shape filled with black, and no stroke, and hence the resulting mask is the shape of the inside of your (incorrect) shape, giving the gradient mask shape that you show. 所以你得到的(不正确的)形状充满黑色,没有笔画,因此产生的面具是你(不正确)形状内部的形状,给出你所显示的渐变面具形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM