gpt4 book ai didi

ios - 渐变 alpha 在 drawRect 中不起作用

转载 作者:行者123 更新时间:2023-11-28 19:04:15 29 4
gpt4 key购买 nike

我使用的是从顶部答案中获取的代码的稍微修改版本:Modern technique of adding gradient to UIView

我修改后的代码:

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

// The if statements are surely a crappy way of setting the locations, but whatever
CGGradientRef gradient;
if ([self.colors count] == 3) {
CGFloat gradientLocations[] = {0, 0.5, 1};
gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, gradientLocations);
} else {
CGFloat gradientLocations[] = {0, 1};
gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, gradientLocations);
}

CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
}

我已将它放入一个名为 Gradient_View 的类中,并且我正在尝试对其进行子类化。在我的子类的 init 方法中,我有这个:

self.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f].CGColor,
[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.0f].CGColor,
nil];

请注意,最终数组中的第二种颜色的 alpha 值为 0,因此渐变应该变得透明。然而,我在模拟器中得到的结果是完全不透明的:

enter image description here

我怎样才能使透明度发挥作用?

最佳答案

检查您的 UIView 是否不透明。确保不透明设置为否。

关于ios - 渐变 alpha 在 drawRect 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21894550/

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