패스 여러 색상 :
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를 사용하는 것보다 그라데이션, 더 눈에 띄는입니다 :
감사합니다. 나는 그것을 간과했다. :/ – jarryd
@ Helium3 오신 것을 환영합니다. – justin