Friday, July 8, 2011

Draw Radial Gradient in Quartz

Here is the code snippet for that to be written inside - (void)drawRect:(CGRect)rect of UIView




    CGContextRef ctx = UIGraphicsGetCurrentContext();

    
    
    //Draw the gray Gradient
    
    CGFloat BGLocations[2] = { 0.0, 1.0 };
    CGFloat BgComponents[8] = { 0.25, 0.25, 0.25 , 1.0// Start color
       0.11, 0.11, 0.11, 1.0 }; // Mid color and End color
    CGColorSpaceRef BgRGBColorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef bgRadialGradient = CGGradientCreateWithColorComponents(BgRGBColorspace, BgComponents, BGLocations, 2);
    
    
    CGPoint startBg = CGPointMake(250, 250); 
    CGFloat endRadius= 250;
    
    
    CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, endRadius, kCGGradientDrawsAfterEndLocation);
    CGColorSpaceRelease(BgRGBColorspace);
    CGGradientRelease(bgRadialGradient);
    







Screenshot of the output

5 comments:

  1. Thông tin thật hưu ích, cảm ơn bạn đã chia sẻ.
    Xem tại Page : Giá đá thạch anh

    ReplyDelete
  2. Kiến thức của Bạn cũng cần thiết, cám ơn bạn đã chia sẻ.
    Page hữu ích : Giá thạch anh vụn

    ReplyDelete
  3. Bài post của Admin rất hay, cám ơn bạn đã chia sẻ.
    Xem tại website : Gia công đá thạch anh

    ReplyDelete
  4. i have created a new file to have this code in cgrect but what if i want to change the end radius. How can i override this function

    ReplyDelete
  5. i have created a new file to have this code in cgrect but what if i want to change the end radius. How can i override this function

    ReplyDelete