Paints a gradient fill that varies along the area defined by the provided starting and ending circles.
void CGContextDrawRadialGradient(
CGContextRef context,
CGGradientRef gradient,
CGPoint startCenter,
CGFloat startRadius,
CGPoint endCenter,
CGFloat endRadius,
CGGradientDrawingOptions options
);
Parameters
context
A Quartz graphics context.
gradient
A CGGradient object.
startCenter
The coordinate that defines the center of the starting circle.
startRadius
The radius of the starting circle.
endCenter
The coordinate that defines the center of the ending circle.
endRadius
The radius of the ending circle.
options
Option flags (kCGGradientDrawsBeforeStartLocation or kCGGradientDrawsAfterEndLocation) that control whether the gradient is drawn before the starting circle or after the ending circle.
Discussion
The color at location 0 in the CGGradient object is mapped to the circle defined by startCenter and startRadius. The color at location 1 in the CGGradient object is mapped to the circle defined by endCenter and endRadius. Colors are linearly interpolated between the starting and ending circles based on the location values of the gradient. The option flags control whether the gradient is drawn before the start point or after the end point.
startCenter和endCenter 应该是径向渐变的圆心的位置吗,那startRadius和endRadius 的开始和结束的半径是什么意思?
画出来的怎么感觉和我想的不一样啊?
我想绘制一个从屏幕中心到周边的由白色到黑色的渐变
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
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
0.0,0.0,0.0,1.0 }; // End color
myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, components,
locations, num_locations);
CGPoint myStartPoint={rect.size.width/2,rect.size.height/2}, myEndPoint={rect.size.width,rect.size.height};
CGFloat myStartRadius=0, myEndRadius=rect.size.width;
CGContextDrawRadialGradient (context, myGradient, myStartPoint,
myStartRadius, myEndPoint, myEndRadius,
kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
CGColorSpaceRelease(myColorspace);
CGGradientRelease(myGradient);