UIView *gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, 45 * kHeightRatio, bgView.width, 3)];
[bgView addSubview:gradientView];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = gradientView.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:51 / 255.0 green:193/255.0 blue:188/255.0 alpha:1.0].CGColor,
(id)[UIColor colorWithRed:34 / 255.0 green:201/255.0 blue:161/255.0 alpha:1.0].CGColor,
(id)[UIColor colorWithRed:65 / 255.0 green:1 blue:234/255.0 alpha:1.0].CGColor, nil];
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(1, 0.5);
gradient.locations = @[@0,@0.4,@1];
[gradientView.layer addSublayer:gradient];
//startPoint和endPoint范围在0到1之间
左上角的,右上角,左下角,右下角的坐标分别为(0,1),(1,1),(0,0),(1,0)
这篇博客介绍了如何在iOS中利用CAGradientLayer为UIView添加从RGB(51, 193, 188)到RGB(34, 201, 161),再到RGB(65, 1, 234)的水平渐变背景。通过设置startPoint和endPoint为(0,0.5)和(1,0.5),实现颜色从左到右平滑过渡。此外,还设置了颜色分布的位置,使渐变效果更加精确。
2895

被折叠的 条评论
为什么被折叠?



