0
어떻게 quartz2d로 그래디언트 선을 그릴 수 있습니까?Quartz2D 및 그래디언트 선
어떻게 quartz2d로 그래디언트 선을 그릴 수 있습니까?Quartz2D 및 그래디언트 선
CGContextDrawLinearGradient
그라디언트를 그립니다. CGContextClipToRect
(또는 관련 클리핑 함수)을 사용하여 그라디언트로 채울 영역을 잘라냅니다.
그라데이션을 사용하여 경로를 칠하거나 채우는 기능이 없으므로 대신 채울 영역을 클립하십시오.
나는 drawRect:
안에이 사용하고 마법처럼 일했다 :
double slope, cosy, siny;
double halfWidth = 2; // the thickness of the line will be 2*halfWith
// the start and end points have to be inside UIView frame coordinates
CGPoint start = CGPointMake(30, 20);
CGPoint end = CGPointMake(80, 90);
slope = atan2((start.y - end.y), (start.x - end.x));
cosy = cos(slope);
siny = sin(slope);
CGContextRef context = UIGraphicsGetCurrentContext();
CGGradientRef myGradient;
CGColorSpaceRef myColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 1.0, // Start color (White)
1.0, 0.0, 0.0, 1.0 }; // End color (Red)
myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
CGContextMoveToPoint(context, start.x -halfWidth*siny , start.y +halfWidth*cosy);
CGContextAddLineToPoint(context, end.x -halfWidth*siny , end.y +halfWidth*cosy );
CGContextAddLineToPoint(context, end.x +halfWidth*siny , end.y -halfWidth*cosy);
CGContextAddLineToPoint(context, start.x +halfWidth*siny, start.y -halfWidth*cosy);
CGContextAddLineToPoint(context, start.x -halfWidth*siny , start.y +halfWidth*cosy);
CGContextClip(context);
CGContextDrawLinearGradient (context, myGradient, start, end, 0);
CGColorSpaceRelease(myColorspace);
CGGradientRelease (myGradient);