CGGradient in an CGPath

偶尔善良 提交于 2019-12-18 03:44:55

问题


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:(NSBezierPath *)path relativeCenterPosition:(NSPoint)relativeCenterPosition of NSGradient.

回答1:


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);



回答2:


Yes, I think so if I understand your question. It is a bit involved but here is a good example of doing it:

http://cocoawithlove.com/2008/09/drawing-gloss-gradients-in-coregraphics.html

This is done with Cocoa and not Cocoa Touch but it translates with everything bu NSColor. You have to use UIColor instead. Basically, you have to create a gradient function and then use:

CGShadingCreateAxial()

to determine the value of your gradient.

Or are you wanting to have a line with a gradient?



来源:https://stackoverflow.com/questions/2563303/cggradient-in-an-cgpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!