简体   繁体   English

CGPath中的CGGradient

[英]CGGradient in an CGPath

Is it possible to draw a gradient in a path on the iPhone? 是否可以在iPhone上的路径中绘制渐变?

I'm looking for a replacement of the mac os x method 我正在寻找mac os x方法的替代品

-(void)drawInBezierPath:(NSBezierPath *)path relativeCenterPosition:(NSPoint)relativeCenterPosition of NSGradient.

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

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 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. 这是通过Cocoa而不是Cocoa Touch完成的,但它可以转换为NSColor的所有内容。 You have to use UIColor instead. 你必须使用UIColor。 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? 或者你想要一个带渐变的线?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM