使用 Core Graphics 在 CGContextClip 之后绘制阴影

发布于 2024-08-24 17:00:44 字数 2129 浏览 21 评论 0原文

更新: 我尝试实现 Peter 指定的方法,但得到了不正确的阴影。怎么了?

替代文本 http://grab.by/2XyP

CGContextSetShadowWithColor(c, CGSizeMake(4, 4), kAudioThumbShadowBlur, [[UAColor blackColor] CGColor]);
    CGContextFillPath(c);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, minx, midy);
    CGPathAddArcToPoint(path, NULL, minx, miny, midx, miny, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, maxx, miny, maxx, midy, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, maxx, maxy, midx, maxy, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, minx, maxy, minx, midy, kDefaultMargin);
    CGPathCloseSubpath(path);


    // Fill and stroke the path
    CGContextSaveGState(c);
    CGContextAddPath(c, path);
    CGContextClip(c);


    myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, 2);
    CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);

    CGContextAddPath(c, path);
    CGPathRelease(path);
    CGContextStrokePath(c);
    CGContextRestoreGState(c);





原问题: 我希望使用以下代码在 CoreGraphics 中制作的自定义圆形单元格项目的底部周围绘制自然的阴影:

...
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, minx, miny);
    CGPathAddArcToPoint(path, NULL, minx, maxy, midx, maxy, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, maxx, maxy, maxx, miny, kDefaultMargin);
    CGPathAddLineToPoint(path, NULL, maxx, miny);
    CGPathAddLineToPoint(path, NULL, minx, miny);
    CGPathCloseSubpath(path);

    // Fill and stroke the path
    CGContextSaveGState(c);
    CGContextAddPath(c, path);
    CGContextClip(c);

    myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, 2);
    CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);

    CGContextAddPath(c, path);
    CGPathRelease(path);
    CGContextStrokePath(c);
    CGContextRestoreGState(c);
...

我想在渐变填充之前或之后在该路径的外部应用阴影。这样做的最佳方法是什么?

UPDATE:
I tried implementing the method specified by Peter and am getting incorrect shadowing. What is wrong?

alt text http://grab.by/2XyP

CGContextSetShadowWithColor(c, CGSizeMake(4, 4), kAudioThumbShadowBlur, [[UAColor blackColor] CGColor]);
    CGContextFillPath(c);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, minx, midy);
    CGPathAddArcToPoint(path, NULL, minx, miny, midx, miny, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, maxx, miny, maxx, midy, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, maxx, maxy, midx, maxy, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, minx, maxy, minx, midy, kDefaultMargin);
    CGPathCloseSubpath(path);


    // Fill and stroke the path
    CGContextSaveGState(c);
    CGContextAddPath(c, path);
    CGContextClip(c);


    myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, 2);
    CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);

    CGContextAddPath(c, path);
    CGPathRelease(path);
    CGContextStrokePath(c);
    CGContextRestoreGState(c);

ORIGINAL QUESTION:
I am looking to draw a natural looking shadow around the bottom of a custom rounded cell item I make in CoreGraphics using this code:

...
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, minx, miny);
    CGPathAddArcToPoint(path, NULL, minx, maxy, midx, maxy, kDefaultMargin);
    CGPathAddArcToPoint(path, NULL, maxx, maxy, maxx, miny, kDefaultMargin);
    CGPathAddLineToPoint(path, NULL, maxx, miny);
    CGPathAddLineToPoint(path, NULL, minx, miny);
    CGPathCloseSubpath(path);

    // Fill and stroke the path
    CGContextSaveGState(c);
    CGContextAddPath(c, path);
    CGContextClip(c);

    myGradient = CGGradientCreateWithColorComponents(myColorspace, components, locations, 2);
    CGContextDrawLinearGradient(c, myGradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), 0);

    CGContextAddPath(c, path);
    CGPathRelease(path);
    CGContextStrokePath(c);
    CGContextRestoreGState(c);
...

I want to apply the shadow around the outside of this path, either before or after the gradient fill. What is the best way to go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2024-08-31 17:00:44

渐变绘制不算作填充,因此,首先设置阴影并进行纯色填充。然后,使用渐变和剪切描边在纯色填充上绘制。

A gradient draw doesn't count as a fill, so, first, set the shadow and do a solid-color fill. Then, draw over the solid-color fill with the gradient and clipped stroke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文