我需要一些帮助来调整带有蒙版的渐变图层 . 所以我有一个CAGradientLayer . 在这一层的顶部,我有一个圆形面具 . 掩模位于梯度层的中心 . 渐变层不断旋转,由于圆形遮罩位于渐变层的中心,因此您可以看到一个不断旋转的圆的错觉 . 一切正常,直到我需要调整渐变图层的大小(并使用蒙版) . 当我尝试同时调整蒙版和渐变层的大小时,会出现很多问题:

  • 圆圈不再围绕它的中心旋转

  • 调整大小是完全错误的(圆圈不完整(遮罩不适合图层大小))

显然,我尝试过调整图层的大小以使其大于面具,但它不起作用 . 我个人认为问题在于轮换 . 面具不会停留在图层的中心,因此它会错误地旋转圆圈 . 我希望你们中的一些人可以帮助我 . 我需要将面具保持在图层的中心 . 谢谢您的帮助 . 啊,是的,调整大小正在“实时”发生,因为我在捏时做到了 .

更新:我将发布用于绘制和动画一切的代码 . 但首先让我解释一些背景信息 . 在视图启动时,我使用CALayers在视图上绘制一些圆圈,每个圆圈有3层:

  • 我已正确动画的背景圆圈

  • 带有蒙版和旋转动画的渐变图层

  • 文本层

捏 - >调整大小动画还有其他问题,例如,如果你捏住它停留在其他圆下面的第一个圆(因为其他圆在层次结构中更高) . 但这是我很容易解决的小问题 . o使用掩码和背景圈为圆圈设置动画我使用此代码,当UIPinchGestureRecognizer的状态为UIGestureRecognizerStateChanged时调用:

//Getting the aleready existing data
CAGradientLayer *gradientLayer = [sender.layer.sublayers objectAtIndex:3]; //takes the   already existing layer with the gradient
CAShapeLayer *outCircle = [sender.layer.sublayers objectAtIndex:2]; //the out circle is the background circle
//creating new mask...
CAShapeLayer *newMask = [CAShapeLayer layer]; //I need to create a new mask for the  gradient layer, since I can't acess the old mask in the sublayers array and I need to scale the size of the mask too.
newMask.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200,  200)].CGPath;
newMask.lineWidth = 10;
newMask.fillColor = [UIColor clearColor].CGColor;
newMask.strokeColor = [UIColor blackColor].CGColor;
//setting new mask...
gradientLayer.mask = newMask;
//the resize animation aleready exists and is created when the state of the    UIPinchGestureRecognizer is UIGestureRecognizerStateBegan
[_resizeAnimation setToValue:[NSNumber numberWithFloat:1+(0.5*scale)]]; //scale is a   property of UIPinchGestureRecognizer that indicates the distance between the two fingers
//adding the animation to the layers
[outCircle addAnimation:_resizeAnimation forKey:nil];
[gradientLayer addAnimation:_resizeAnimation forKey:nil];
[newMask addAnimation:_resizeAnimation forKey:nil];

代码位于一个方法内,该方法还将发送方UIView作为参数来访问图层(以及比例值) . 以下是初始化动画的代码:

_resizeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[_resizeAnimation setDuration:1/scale];
_resizeAnimation.fillMode = kCAFillModeForwards;
[_resizeAnimation setRemovedOnCompletion:NO];

我在捏开始时启动动画 . 我这样做是因为我不想重新分配并创建一个新的动画,每次捏合的值改变时,它都会像前一个一样 . 所以这就是代码 . 谢谢大家帮助我!

更新:我解决了我的问题 . 这很容易,但动画仍然不完美 . 我解决了编辑2件事的问题:

-removed应用于蒙版的动画-added gradientLayer.masksToBounds = YES;

还有一些东西不能正常工作:outCircle(背景)根据它的框架矩形原点调整大小 . 另一方面,渐变层根据其中心调整大小 . 我想根据中心和背景层调整大小 . 问题似乎是锚点 . 但默认情况下,图层的锚点是中心(0.5,0.5),就像在渐变图层中一样 . 我从来没有在我的代码中改变outCircle的锚点 . 那么为什么outCircle的锚点是错的呢?我尝试手动设置为0.5,0.5,但没有任何变化 . 请帮忙!