iphone - 使用 UIBezierPath 中的 CAShapeLayer 进行屏蔽时,CAGradientLayer 出现意外输出

标签 iphone ios core-graphics cashapelayer cagradientlayer

我正在使用this sample code来自Palimondo :这是学习如何将 CAGradientLayer 屏蔽到 CAShapeLayers 上的精彩代码。输出如下:

enter image description here

现在我正在尝试创建此图像:

enter image description here

使用 PaintCode,我能够获取所有 UIBezierPath 和渐变颜色并停止并将它们放入多个数组中,并基本上迭代每个数组,将其添加到新的 CAShapeLayer 和 CAGradientLayer 中,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self blackPaths];
    [self setupGradients];
    int count = 0;
    for (CAGradientLayer *gradientLayerItem in _individualGradientArrays) {

        CAShapeLayer *gradientMask = [CAShapeLayer layer];
        gradientMask.fillColor = [[UIColor clearColor] CGColor];
        gradientMask.strokeColor = [[UIColor blackColor] CGColor];
        gradientMask.lineWidth = 4;
        gradientMask.frame = CGRectMake(0, 0, _testView.bounds.size.width, _testView.bounds.size.height);
        //
        //    CGMutablePathRef t = CGPathCreateMutable();
        //    CGPathMoveToPoint(t, NULL, 0, 0);
        //    CGPathAddLineToPoint(t, NULL, _testView.bounds.size.width, _testView.bounds.size.height);

        gradientMask.path = [[_UIBezierPathsArray objectAtIndex:count]CGPath];


        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.locations = gradientLayerItem.locations;
        gradientLayer.startPoint = CGPointMake(0.5,1.0);
        gradientLayer.endPoint = CGPointMake(1.0,0.5);
        gradientLayer.frame = CGRectMake(0, 0, _testView.bounds.size.width, _testView.bounds.size.height);
        //    NSMutableArray *colors = [NSMutableArray array];
        //    for (int i = 0; i < 3; i++) {
        //        [colors addObject:(id)[[UIColor colorWithHue:(0.2 * i) saturation:1 brightness:.8 alpha:1] CGColor]];
        //    }
        gradientLayer.colors = gradientLayerItem.colors;

        [gradientLayer setMask:gradientMask];
        [_testView.layer addSublayer:gradientLayer];
        count++;
    }

[_testView setNeedsDisplay];

目标是拥有 16 个单独的 CAGradientLayer,我可以使用 CoreAnimation 对其进行动画处理,以创建一种“旋转”效果(我可以将 PaintCode 的输出放入到一个 drawRect 中,并且它可以工作,问题是我被告知与将它们放置在图层中相比,通过 CoreGraphics 对颜色或渐变进行动画处理要困难得多)。

然后,输出如下:

The 70's dance party version...

经过 100 次迭代之后,在此之前,我尝试将 [UIColor clearColor] 替换为 whiteColor (这不是示例代码所需的),这是输出:

almost there...

您可以看到只有几个渐变,我相信 whiteColor 会干扰 CAGradientLayer。

我知道我一定错过了一些极其微妙的东西,但我似乎看不到它。

为什么在示例代码中,我们能够使用 clearColor 并适当显示渐变,但使用 clearColor 并将我的 UIBezierPath 附加到我的 CAShapeLayer渐变中断?

一位 friend 提到,另一种解决方案是使用 3 个渐变圆圈,并基本上在蒙版形状上同心旋转它们,从而产生幻觉,但我不知道如何才能做到这一点。

最佳答案

很难从这里确切地说出到底发生了什么。在第三张图片中(基于您的代码),您将在 mask 中的透明填充周围绘制不透明的黑色笔划,因此当您将其用作 mask 时,只有您描画的区域将从渐变层中“可见”(这些只是笔划的提示是您可以看到笔划斜接)。当您将clearColor更改为whiteColor(不透明)时,路径内的区域也从渐变层中变得可见(提示是由于笔划沿着路径的中间延伸,所以线条显得更粗,从而使填充“变胖”。此外,笔划斜接不再可见,因为它们已被填充遮挡。)

在 mask 层中,颜色无关紧要——重要的是 Alpha channel (不透明度)。这就是为什么白色与黑色没有区别。它们的 alpha 值为 1.0,而clearColor 的 alpha 值为 0.0。从标题:

/* A layer whose alpha channel is used as a mask to select between the
 * layer's background and the result of compositing the layer's
 * contents with its filtered background. Defaults to nil. When used as
 * a mask the layer's `compositingFilter' and `backgroundFilters'
 * properties are ignored. When setting the mask to a new layer, the
 * new layer must have a nil superlayer, otherwise the behavior is
 * undefined. */

@property(retain) CALayer *mask;

这就是为什么您不再在第四张图像中看到笔划斜角的原因 - 它们被隐藏,因为 mask 仅查看 Alpha channel ,并且无论色调(即白色或黑色)它们都绘制alpha 为 1.0。

同样,目前还不清楚您最终要在这里做什么,但您对白色颜色的使用感觉像是转移注意力。如果您的目标是看起来像第二个图像,您可能希望蒙版层没有笔划和不透明填充,如下所示:

gradientMask.fillColor = [[UIColor blackColor] CGColor];
gradientMask.strokeColor = [[UIColor clearColor] CGColor];

关于iphone - 使用 UIBezierPath 中的 CAShapeLayer 进行屏蔽时,CAGradientLayer 出现意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17935112/

相关文章:

iphone - UIView 层的内阴影效果?

iPhone + CGAffineTransFormRotate(pi/2) + statusBarHidden :YES + presentModalViewController = 20 pixels of white space

ios - 任何使用 Java 脚本的 iPhone UI 自动化教程?

iphone - iTunes Connect,查看总销售额?

objective-c - CGContextDrawImage 导致 EXC BAD ACCESS

ios - 基于最新 iOS 功能的位置通知

iphone - 创建一个iOS6 Maps MKUserTrackingBarButtonItem按钮

iphone - 如何从核心数据库获取数据

ios - 带有来自 plist 的数据的 objective-c 过滤 TableView

ios - 如何根据我的需要创建一个新的 iOS 框架?