2012-07-30 3 views
1

3 가지 색상의 CGGradient를 어떻게 그리나요?CGGradient (3 색)

나는 배열과 같이 있습니다

CFArrayRef colors = (__bridge CFArrayRef) [NSArray arrayWithObjects:(id)lightGradientColor.CGColor, 
              (id)darkGradientColor.CGColor, (id)lightGradientColor.CGColor, 
              nil]; 

하지만 상단과 하단 부분에 빛을 중간에 어두운 색깔을보고 해달라고 아니라 단지 바닥까지 어둠으로, 위에 켜집니다.

답변

3

색상 위치를 지정/확인하려 했습니까? 범위 [0 ... 1]이다

const CGFloat locations[3] = {0.0, 0.5, 1.0}; 
CGGradientRef grad = CGGradientCreateWithColors(colorspace, colors, locations); 

참고 : 위의 위치가 locations 매개 변수 0 전달과 동일해야한다. 부드러운 결과를 CGGradient 대신 CAGardientLayer를 사용

+0

감사합니다. 나는 그것을 간과했다. :/ – jarryd

+0

@ Helium3 오신 것을 환영합니다. – justin

0

패스 여러 색상 :

A. 헤더에서 @property있는 NSArray * 색상으로 사용자 정의 UIView의 클래스를 만듭니다. 구현 파일에서 다음 drawRect 메소드를 붙여 넣으십시오. B에의 ViewController에서

-(void)drawRect:(CGRect)rect { 

    //1. create vars 
    float increment = 1.0f/(colours.count-1); 
    CGFloat * locations = (CGFloat *)malloc((int)colours.count*sizeof(CGFloat)); 
    CFMutableArrayRef mref = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); 

    //2. go through the colours, creating cgColors and locations 
    for (int n = 0; n < colours.count; n++){ 
     CFArrayAppendValue(mref, (id)[colours[n] CGColor]); 
     locations[n]=(n*increment); 
    } 

    //3. create gradient 
    CGContextRef ref = UIGraphicsGetCurrentContext(); 
    CGColorSpaceRef spaceRef = CGColorSpaceCreateDeviceRGB(); 
    CGGradientRef gradientRef = CGGradientCreateWithColors(spaceRef, mref, locations); 
    CGContextDrawLinearGradient(ref, gradientRef, CGPointMake(0.0, 0.0), CGPointMake(0.0, self.frame.size.height), kCGGradientDrawsAfterEndLocation); 
    CGColorSpaceRelease(spaceRef); 
    CGGradientRelease(gradientRef); 
} 

당신은 그것을 초기화, 사용자 정의 클래스를 사용하는 프레임의 색상을 설정합니다. 그것은 하나 이상의 색상을 위해 작동하며,이 경우 위에서 아래로 실행됩니다. 당신은 색상에 알파를 떨어 뜨리면

Background * bg = [Background new]; 
[bg setFrame:self.view.bounds]; 
[bg setColours:@[[UIColor blueColor],[UIColor purpleColor]]]; 
[self.view addSubview:bg]; 

IT는 매끄럽게 CAGradientLayer를 사용하는 것보다 그라데이션, 더 눈에 띄는입니다 :

CGGradient CAGradientLayer