【问题标题】:Filling a shape with a gradient in CGContext在 CGContext 中用渐变填充形状
【发布时间】:2009-06-22 08:06:30
【问题描述】:

我想用线性 CGGradient 填充我通过 Core Graphics 绘制的多边形形状。 CGContextDrawLinearGradient 函数绘制从一个点到另一个点的渐变,但它会填充整个视图。如何仅在我绘制的多边形内显示渐变?

【问题讨论】:

    标签: objective-c cocoa cgcontext


    【解决方案1】:

    您可以构造一个您想要的形状的 CGMutablePath,然后使用它来剪辑到您想要显示的区域,例如...

    // Construct yourClipPath
    
    CGContextAddPath(yourContext, yourClipPath);
    CGContextClosePath(yourContext);
    CGContextClip(yourContext);
    
    // Draw Your Gradient
    

    【讨论】:

    • 谢谢,这行得通!我发现我必须调用 CGContextSaveGState/CGContextRestoreGState 来取消剪辑该区域。
    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多