나는 왜 이것이 작동하지 않는지를 파악하려고 오랫동안 벽을 머리에 대고 머리를 두드렸다.CGPath에 CGPath를 클리핑
기본적으로 CGPath를 사용하여 그래프 (차트)를 플로팅 한 다음이를 사용하여 그라디언트를 클리핑하려고합니다. 최종 효과는 iPhone과 함께 제공되는 주식 앱과 같아야합니다.
그라디언트와 패스는 두 개의 레이어 된 (분리되지 않은) 요소로 별도로 정밀하게 그립니다. 그러나 CGContextDrawPath를 주석 처리하면 선이나 그라디언트가 화면에 표시되지 않습니다. 이 단지 일련의 선 세그먼트을 나타내는
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor whiteColor] set];
CGContextSetLineWidth(context, 2.0f);
CGPoint lastDrawnPt = [[points objectAtIndex:0] CGPointValue];
CGPoint firstDrawnPt = lastDrawnPt;
//create the path for the gradient
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, lastDrawnPt.x, self.bounds.size.height); // bottom left
CGPathAddLineToPoint(thePath, NULL, lastDrawnPt.x, lastDrawnPt.y);
for (int i=0; i<(points.count-1); i++) {
//CGPoint pt1 = [[points objectAtIndex:i] CGPointValue];
CGPoint pt2 = [[points objectAtIndex:i+1] CGPointValue];
if (pt2.x > lastDrawnPt.x+2) {
// only draw if we've moved sunstantially to the right
//for the gradient
CGPathMoveToPoint(thePath, NULL, lastDrawnPt.x, lastDrawnPt.y);
CGPathAddLineToPoint(thePath, NULL, pt2.x, pt2.y);
lastDrawnPt = pt2;
}
}
//finish the gradient clipping path
CGPathMoveToPoint(thePath, NULL, lastDrawnPt.x, lastDrawnPt.y);
CGPathAddLineToPoint(thePath, NULL, lastDrawnPt.x, self.bounds.size.height); // bottom right
CGPathMoveToPoint(thePath, NULL, lastDrawnPt.x, self.bounds.size.height);
CGPathAddLineToPoint(thePath, NULL, firstDrawnPt.x, self.bounds.size.height); // bottom right
CGPathCloseSubpath(thePath);
//add the gradient clipping path to the context
CGContextSaveGState(context);
CGContextAddPath(context, thePath);
//draw the path
float components[4] = {1.0, 1.0, 1.0, 1.0};
CGContextSetStrokeColor(context, components);
CGContextDrawPath(context,kCGPathStroke);
//clip the path
CGContextClip(context);
//Draw Gradient
UIColor *topColor = [UIColor colorWithRed: 1.0 green:1.0 blue:1.0 alpha:1.0];
UIColor *bottomColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0];
CGColorRef colorRef[] = { [topColor CGColor], [bottomColor CGColor] };
CFArrayRef colors = CFArrayCreate(NULL, (const void**)colorRef, sizeof(colorRef)/sizeof(CGColorRef), &kCFTypeArrayCallBacks);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colors, NULL);
CFRelease(colorSpace);
CFRelease(colors);
// Draw a linear gradient from top to bottom
CGPoint gradStartPoint = CGPointMake(50.0, self.bounds.size.height);
CGPoint gradEndPoint = CGPointMake(50.0, 0.0);
CGContextDrawLinearGradient(context, gradient, gradStartPoint, gradEndPoint, 0);
CFRelease(gradient);
// Cleanup
CGColorSpaceRelease(colorSpace);
CGContextRestoreGState(context);
CGContextFlush 또는 CGContextSynchronize를 시도 했습니까? 레이어링과 관련이 있다고 생각합니다. 그래디언트가 그려진 후에도 CGContextClip을 움직여보십시오. – namar0x0309
아이디어를 주셔서 감사합니다. 그러나 그들은 도움이되지 않았습니다. – avance
나는 내 길을 만드는 방식에 뭔가 문제가 있다고 생각하기 시작했습니다. – avance