Check out my example, it interpolates the colors based on my UISliders position values 0<=slider.value<=1.0f. If your CAGradientLayer's start and endpoint are different, you will have to change how the mapping works out. I think for the standard start and end point the gradient goes from top left to bottom right, in which case if you think about the layer as a rectangle which the color stripes are going diagonally, all you need to do is rotate the rectangle 45 degrees and then only use the x or y positions(depending on how you rotated) which should allow you to use this method.
-(UIColor *) getColor{
/* //My pre declared code(just for clarity)
@property (weak, nonatomic) IBOutlet UISlider *slider;
CAGradientLayer* gradientLayer;
float colorCutoff;
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor orangeColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor cyanColor].CGColor,(__bridge id)[UIColor blueColor].CGColor,(__bridge id)[UIColor purpleColor].CGColor];
//This below allows for easy one direction interpolation
gradientLayer.startPoint = CGPointMake(0.5,0.0);
gradientLayer.endPoint = CGPointMake(0.5,1.0f);
colorCutoff = 1/gradientLayer.colors.count;
*
*
*/
float interp = _slider.value;
// 1.0f * colors.count == index out of bounds
if(interp >= 1.0f){
interp = .99999f;
}
// think FLOOR(float * float)
int firstColor = (interp * (float)gradientLayer.colors.count);
int secondColor = firstColor +1;
// In case we are at the last color their is not one above it(index out of bounds)
if(secondColor >= gradientLayer.colors.count){
firstColor--;
secondColor--;
}
// In My case the closer you are to the colorCuttoff the more the second color should have a factor
// 0 is red .14 is orange .28 is yellow and soo on
float firstInterp = 1.0f - (fmodf(interp,colorCutoff) / colorCutoff);
float secondInterp = 1.0f - firstInterp;
// make use of the gradientLayer.colors array
const CGFloat *firstCGColor = CGColorGetComponents((__bridge CGColorRef)gradientLayer.colors[firstColor]);
const CGFloat *secondCGColor = CGColorGetComponents((__bridge CGColorRef)gradientLayer.colors[secondColor]);
float red = (firstCGColor[0] * firstInterp) + secondCGColor[0] * secondInterp;
float green = (firstCGColor[1] * firstInterp) + secondCGColor[1] * secondInterp;
float blue = (firstCGColor[2] * firstInterp) + secondCGColor[2] * secondInterp;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}