Docs say:
The gradient stops are specified as values between 0 and 1. The values must be monotonically increasing.
So locations
have actually nothing to do with gradient orientation. Regarding the latter refer to this question. Locations mean location where gradient stop, eg. in first view red begins at 0 (top) and ends at 0.5 (middle), so further to bottom can be only solid blue. If you give [0.5, 0.5], it means, that both gradients should begin and end in the middle, so the colors don't mix at all.
Code producing the below gradients:
@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
