V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
adow
V2EX  ›  iDev

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

  •  
  •   adow ·
    adow · 2012-12-12 11:54:01 +08:00 · 8354 次点击
    这是一个创建于 4375 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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);
    3 条回复    1970-01-01 08:00:00 +08:00
    doskoi
        1
    doskoi  
       2012-12-12 16:12:25 +08:00   ❤️ 1
    void CGContextDrawRadialGradient(
    CGContextRef context,
    CGGradientRef gradient, //先创造一个CGGradientRef,颜色是白,黑,location分别是0,1
    CGPoint startCenter, // 白色的起点(中心圆点)
    CGFloat startRadius, // 起点的半径,这个值多大,中心就是多大一块纯色的白圈
    CGPoint endCenter, // 白色的终点(可以和起点一样,不一样的话就像探照灯一样从起点投影到这个终点,按照你的意图应该和startCenter一样
    CGFloat endRadius, //终点的半径, 按照你的意图应该就是从中心到周边的长
    CGGradientDrawingOptions options //应该是 kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation
    );
    doskoi
        2
    doskoi  
       2012-12-12 16:22:32 +08:00
    adow
        3
    adow  
    OP
       2012-12-12 16:55:05 +08:00
    这回懂了,谢谢@doskoi
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1046 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 19:23 · PVG 03:23 · LAX 11:23 · JFK 14:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.