【问题标题】:Poor drawing of CGGradient with alpha带有 alpha 的 CGGradient 绘制不佳
【发布时间】:2016-01-19 05:15:22
【问题描述】:

我最终尝试创建一个渐变叠加层,从 60% 黑色渐变到透明,再到 40% 黑色。结果有很多条带,所以我只是想先让两种颜色正常工作。

在第一种情况下,我正在尝试从纯黑色到纯白色的渐变,效果非常完美。

在第二种情况下,我在纯灰色背景下放置了一个从 100% 黑色到透明的渐变。绑带真的很糟糕。

制作此渐变的代码如下所示:

// Draw the overlay gradient
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);

UIColor *firstColor = [UIColor colorWithWhite:0.0 alpha:1.0];       
UIColor *secondColor = [UIColor clearColor];
CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

NSArray *coreGraphicsColors = @[(id)firstColor.CGColor, (id)secondColor.CGColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)coreGraphicsColors, 0);

static const CGFloat standardDegree = 270;
const CGFloat degree = (standardDegree * M_PI / 180);
const CGPoint center = CGPointMake(size.width / 2, size.height / 2);
const CGPoint startPoint = CGPointMake(center.x - cos(degree) * size.width / 2, center.y - sin(degree) * size.height / 2);
const CGPoint endPoint = CGPointMake(center.x + cos(degree) * size.width / 2, center.y + sin(degree) * size.height / 2);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);

有没有人在创建这些部分透明的渐变方面有一些经验/技巧?我试过改变一些东西,比如位置和色彩空间,但都无济于事。

【问题讨论】:

  • 你能张贴一张结果的照片吗?
  • @Fonix 该代码生成第二个屏幕截图。
  • 哦,抱歉,出于某种原因,我不想为我加载图片

标签: ios core-graphics


【解决方案1】:

这可能会帮助你AlphaGradientView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多