CGGradient in CGPath

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

I am looking for a replacement for the mac os x method

-(void)drawInBezierPath:(NSBezierPath *)path relativeCenterPosition:(NSPoint)relativeCenterPosition of NSGradient. 
+4
source share
2 answers

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); 
+12
source

Yes, I think so, if I understand your question. This is a bit related, but here is a good example of this:

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

This is done using Cocoa, not Cocoa Touch, but it translates anything, NSColor. Instead, you should use UIColor. Basically, you need to create a gradient function, and then use:

 CGShadingCreateAxial() 

to determine the value of your gradient.

Or do you want to have a line with a gradient?

+1
source

Source: https://habr.com/ru/post/1305795/


All Articles