0

我有以下代码,但缺少如何将我的 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];
4

1 回答 1

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 };
于 2013-08-21T07:19:00.163 回答