gpt4 book ai didi

ios - 自定义 View 的渐变颜色

转载 作者:行者123 更新时间:2023-11-28 22:30:44 26 4
gpt4 key购买 nike

我在 UIViewController 中创建了 3 个自定义 View 。在每个自定义 View 中,我都使用 CAShapeLayer 创建了形状

这些图层还包含渐变色。现在我想将渐变颜色设置为自定义 View 。当我尝试这样做时,它正在崩溃。对于第一个 View ,这里是代码:

//first component
self.aTimeScaleMonthView = [[TimeScaleView alloc] initWithFrame:CGRectMake(ORIGIN_X, frame.origin.y, frame.size.width-(2*ORIGIN_X), HEIGHT_OF_COMPONENT1) withStartDate:startDate endDate:endDate];
self.aTimeScaleMonthView.modeOfScale = A3TimeScaleMonth;
self.aTimeScaleMonthView.layer.borderColor = [UIColor blackColor].CGColor;
self.aTimeScaleMonthView.layer.borderWidth = BORDER_WIDTH_BOX;

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.aTimeScaleMonthView.bounds;
gradient.colors = [NSArray arrayWithObjects:[UIColor colorWithRed:0.66 green:0.29 blue:0.22 alpha:1.0], [UIColor colorWithRed:0.62 green:0.51 blue:0.314 alpha:1.0], nil];

[self.aTimeScaleMonthView.layer insertSublayer:gradient atIndex:0];
[self addSubview: self.aTimeScaleMonthView];

请帮帮我。

最佳答案

渐变色应该是 CGColor: gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0.66 green:0.29 blue:0.22 alpha:1.0].CGColor, (id )[UIColor colorWithRed:0.62 green:0.51 blue:0.314 alpha:1.0].CGColor, nil];

顺便说一句,您忘记设置渐变的起点和终点。

关于ios - 自定义 View 的渐变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17542864/

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