请教一下 CGContextDrawRadialGradient 绘制径向渐变的参数是啥意思啊

2012-12-12 11:54:01 +08:00
 adow
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);
8335 次点击
所在节点    iDev
3 条回复
doskoi
2012-12-12 16:12:25 +08:00
void CGContextDrawRadialGradient(
CGContextRef context,
CGGradientRef gradient, //先创造一个CGGradientRef,颜色是白,黑,location分别是0,1
CGPoint startCenter, // 白色的起点(中心圆点)
CGFloat startRadius, // 起点的半径,这个值多大,中心就是多大一块纯色的白圈
CGPoint endCenter, // 白色的终点(可以和起点一样,不一样的话就像探照灯一样从起点投影到这个终点,按照你的意图应该和startCenter一样
CGFloat endRadius, //终点的半径, 按照你的意图应该就是从中心到周边的长
CGGradientDrawingOptions options //应该是 kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation
);
doskoi
2012-12-12 16:22:32 +08:00
adow
2012-12-12 16:55:05 +08:00
这回懂了,谢谢@doskoi

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/54702

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX