如何在iOS中使用渐变位置?
我正在做一个在Xcode中玩颜色的小项目。我想知道梯度位置的作用 我注意到gradientLayer.locations=[0.0,1.0]将使其垂直,所以我认为gradientLayer.locations=[0.5,0.5]将使其水平 [0.5,0.5]-导致两块颜色相互叠加,没有梯度 [0.0,1.0]-我正在寻找的渐变结果,2种垂直颜色,带有渐变 我知道这有点模糊,但我想在这里找到一个解释。这对我来说没什么意义 谢谢, 马特说: 渐变停止点指定为介于0和1之间的值。这些值必须是单调递增的 因此,位置实际上与梯度方向无关。关于后者,请参阅问题。位置是指渐变停止的位置,例如,在第一个视图中,红色从0(顶部)开始,到0.5(中间)结束,因此距离底部较远的位置只能是纯蓝色。如果你给出[ 0.5,0.5 ],这意味着两个渐变都应该开始并在中间结束,所以颜色根本不混合。 生成以下渐变的代码:如何在iOS中使用渐变位置?,ios,colors,hex,gradient,Ios,Colors,Hex,Gradient,我正在做一个在Xcode中玩颜色的小项目。我想知道梯度位置的作用 我注意到gradientLayer.locations=[0.0,1.0]将使其垂直,所以我认为gradientLayer.locations=[0.5,0.5]将使其水平 [0.5,0.5]-导致两块颜色相互叠加,没有梯度 [0.0,1.0]-我正在寻找的渐变结果,2种垂直颜色,带有渐变 我知道这有点模糊,但我想在这里找到一个解释。这对我来说没什么意义 谢谢, 马特说: 渐变停止点指定为介于0和1之间的值。这些值必须是单调递增的
@interface TestViewController ()
@property (strong, nonatomic) IBOutlet UIView *view1;
@property (strong, nonatomic) IBOutlet UIView *view2;
@property (strong, nonatomic) IBOutlet UIView *view3;
@property (strong, nonatomic) IBOutlet UIView *view4;
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *views = @[_view1, _view2, _view3, _view4];
for (UIView *view in views) {
view.layer.borderWidth = 1;
}
// 1
CAGradientLayer *gradient = [CAGradientLayer new];
gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor];
gradient.frame = _view1.bounds;
gradient.locations = @[@0.0, @0.5];
[_view1.layer insertSublayer:gradient atIndex:0];
// 2
gradient = [CAGradientLayer new];
gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor];
gradient.frame = _view2.bounds;
gradient.locations = @[@0.5, @1.0];
[_view2.layer insertSublayer:gradient atIndex:0];
// 3
gradient = [CAGradientLayer new];
gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor, (id)[UIColor greenColor].CGColor];
gradient.frame = _view2.bounds;
gradient.locations = @[@0.0, @0.5, @1.0];
[_view3.layer insertSublayer:gradient atIndex:0];
// 4
gradient = [CAGradientLayer new];
gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor, (id)[UIColor greenColor].CGColor];
gradient.frame = _view4.bounds;
gradient.locations = @[@0.0, @0.8, @1.0];
[_view4.layer insertSublayer:gradient atIndex:0];
}
@end
这是一个非常古老的答案,遇到这个问题,我想我应该为Swift更新已被接受的答案
for view in stackView.arrangedSubviews {
view.layer.borderWidth = 1
view.layer.borderColor = UIColor.black.cgColor
}
// 1
var gradient: CAGradientLayer = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
gradient.frame = view1.bounds
gradient.locations = [0.0, 0.5]
view1.layer.insertSublayer(gradient, at: 0)
// 2
gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
gradient.frame = view2.bounds
gradient.locations = [0.5, 1.0]
view2.layer.insertSublayer(gradient, at: 0)
// 3
gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor, UIColor.green.cgColor]
gradient.frame = view3.bounds
gradient.locations = [0, 0.5, 1.0]
view3.layer.insertSublayer(gradient, at: 0)
// 4
gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor, UIColor.green.cgColor]
gradient.frame = view4.bounds
gradient.locations = [0, 0.8, 1.0]
view4.layer.insertSublayer(gradient, at: 0)
我已经用GitHub链接为所有感兴趣的人做了备份很好的解释和很好的演示,有足够的信息可以尝试使用swift实现。谢谢。啊,绿色的小复选标记?一定是这样。我有点困惑,但我明白了,谢谢@马特卡特利尔:是的,此外,如果你检查一个答案是否被接受,你也会获得一些声誉;)