CGGradient in an CGPath

后端 未结 2 958
無奈伤痛
無奈伤痛 2020-12-16 07:20

Is it possible to draw a gradient in a path on the iPhone?

I\'m looking for a replacement of the mac os x method

-(void)drawInBezierPath:(NSBezierPa         


        
2条回答
  •  半阙折子戏
    2020-12-16 07:46

    I think something like this will work:

    CGContextSaveGState(c);
    CGContextAddPath(c, path);
    CGContextClip(c)
    
    // make a gradient
    CGColorRef colors[] = { topColor, bottomColor };
    CFArrayRef colorsArr = CFArrayCreate(NULL, (const void**)colors, sizeof(colors) / sizeof(CGColorRef), &kCFTypeArrayCallBacks);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colorsArr, NULL);
    CFRelease(colorSpace);
    CFRelease(colorsArr);
    
    //  Draw a linear gradient from top to bottom
    CGPoint start = ...
    CGPoint end = ...
    CGContextDrawLinearGradient(c, gradient, start, end, 0);
    
    CFRelease(gradient);
    CGContextRestoreGState(c);
    

提交回复
热议问题