目标c:用另一层遮盖一层并更改框架时的奇怪行为

问题描述

UIBezierPath * path                 = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:80 startAngle:0 endAngle:(2 * M_PI) clockwise:YES];

CAShapeLayer * layer                = [CAShapeLayer new];
layer.strokeColor                   = [UIColor redColor].CGColor;
layer.fillColor                     = [UIColor clearColor].CGColor;
layer.linewidth                     = 10;
layer.strokeEnd                     = 1;
layer.position                      = self.view.center;
layer.lineCap                       = kCALineCapRound;
[layer setPath                      : path.CGPath];
[self.view.layer addSublayer        : layer];

CAGradientLayer * gradientLayer     = [CAGradientLayer layer];
gradientLayer.startPoint            = CGPointMake(0.0,0.5);
gradientLayer.endPoint              = CGPointMake(1.0,0.5);
gradientLayer.frame                 = self.view.frame;
NSArray *colors                     = @[(id)[UIColor greenColor].CGColor,(id)[UIColor blueColor].CGColor,(id)[UIColor yellowColor].CGColor];
gradientLayer.colors                = colors;
[gradientLayer setMask              : layer];
[self.view.layer addSublayer        : gradientLayer];

我在做什么:

将CAShapeLayer添加到view.layer中,并带有绘制圆的路径。

将CAGradientLayer(3种颜色)添加到view.layer并将遮罩设置为CAShapeLayer。

结果:带和不带面具

enter image description here

enter image description here

问题:

如果我将渐变的框更改为圆的顶部(因为我想查看圆上的所有颜色),它也会移动圆。 特别是渐变的x和y的变化

所以不是

    gradientLayer.frame = self.view.frame;

我只更改x和y(就我检查而言,width和hight不会引起此问题)

    gradientLayer.frame = CGRectMake(100,100,self.view.frame.size.width,self.view.frame.size.height);

这就是结果

enter image description here

任何人都可以解释为什么会发生这种情况以及可能的解决方案吗?

我要实现的目标是

enter image description here

然后使用CAShapeLayer对其进行掩盖,但随后会发生此问题

预先感谢

解决方法

更改shapelayer的位置,因为渐变层是全屏的,并且shapelayer设置了中心位置,并且在更改渐变层的框架后,shapelayer的位置也相同。
layer.position = CGPointMake(30,250);