我有以下代码,但我不知道如何将我的UIColor *放入gradient.colors数组中:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.frame;
UIColor *lightGreen = [UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f];
UIColor *darkGreen = [UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f];
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor lightGrayColor]CGColor], (id)[[UIColor darkGrayColor]CGColor], nil];
[self.view.layer addSublayer:gradient];
答案 0 :(得分:0)
我按照Apple样本的方式这样做了。它用于覆盖UIView上的drawRect。不是很抽象,但它有效。
CGGradientRef myGradient;
CGColorSpaceRef myColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat comps[8];
const CGFloat *start = CGColorGetComponents(startColor.CGColor);
for (int i=0; i<4; i++) {
comps[i] = start[i];
}
const CGFloat *end = CGColorGetComponents(endColor.CGColor);
for (int i=0; i<4; i++) {
comps[i+4] = end[i];
}
myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, comps,
locations, num_locations);
对于将CGColorRef
个对象添加到数组的简单任务:
CGColorRef colorArray[2] = { lightGreen.CGColor, darkGreen.CGColor };