6 1
6 1

Reputation: 1074

How to draw gradient along the arc

I am developing a Mac app, I want to draw a view like a radar, I find no method to draw gradient color along the arc. The existing method only draw gradient towards one direction.

Upvotes: 1

Views: 571

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96323

What you want is an angle gradient. I've written a Core Image filter that generates an angle gradient; give it an opaque color (e.g., green) for the start color and the completely-transparent version of that color for the middle and end colors.

Angle gradient with green, transparent green, and transparent green.

(The filter's output is actually infinite in extent and centered at the origin, so you'll need to mask it out to a circle and use an affine transform at one level or another to get it into the right position.)

Extra credit: In the kernel code for that filter (near the start of the .m), there's a line that starts the gradient at straight-up (90°) rather than straight-right. You could change the code, both of the filter and of the kernel, to make this a parameter (like inputStartColor et al) that you could vary over time, using a CABasicAnimation or something similar.

Upvotes: 5

Wain
Wain

Reputation: 119031

You'll need to use a combination of axial and radial gradients and probably also some clipping paths. You can find all of this documented here.

You can also use colorWithPatternImage: to stroke any lines you're drawing.

Upvotes: 0

Related Questions