gpt4 book ai didi

iphone - 如何用Core Graphics制作渐变效果?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:48 25 4
gpt4 key购买 nike

我想制作一种效果,就像您在此处第一个单元格的右侧看到的那样:

enter image description here

我猜它是某种带有渐变的叠加 View ,但我无法弄清楚它是如何配置透明度的。我尝试制作自己的带有渐变的叠加 View 并降低颜色的 alpha,但它只是显示为灰白色渐变。

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

UIColor *gradBegin = [UIColor colorWithWhite:1.0 alpha:0.8];
UIColor *gradEnd = [UIColor colorWithWhite:1.0 alpha:0.6];
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)gradBegin.CGColor,
(id)gradEnd.CGColor, nil];
CGFloat gradientLocations[] = {0, 1};

CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);

CGContextDrawLinearGradient(context, gradient, CGPointMake(rect.origin.x, rect.origin.y + rect.size.height/2.0),
CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height/2.0), 0);

此屏幕截图中到底发生了什么,我该如何复制它?

最佳答案

我写了一个简单的 UIView 类,它会自己绘制褪色。这是一个覆盖了 drawRect 的基本 UIView:

- (void)drawRect:(CGRect)rect
{
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

UIColor* gradBegin = [UIColor colorWithWhite:1 alpha:0];
UIColor* gradEnd = [UIColor colorWithWhite:1 alpha:1];
NSArray* gradColours = [NSArray arrayWithObjects:
(id)gradBegin.CGColor,
(id)gradBegin.CGColor,
(id)gradEnd.CGColor,
(id)gradEnd.CGColor, nil];
CGFloat gradLocs[] = { 0, 0.5, 0.9, 1 };
CGGradientRef gradient = CGGradientCreateWithColors(colourSpace, (__bridge CFArrayRef)gradColours, gradLocs);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(self.frame.size.width, 0), 0);
CGGradientRelease(gradient);
CGColorSpaceRelease(colourSpace);
}

如果覆盖在您的 View 上并且 View 的背景为白色,则此方法有效。

关于iphone - 如何用Core Graphics制作渐变效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com