Drawing an angle/angular/“Conical”/“Arcing” gradient in Objective-C (IOS) using Core Graphics

后端 未结 3 587
轮回少年
轮回少年 2020-12-19 17:31

I\'m trying to draw a \"conical\"/\"arcing\" gradient (I don\'t know what would be the correct term for this) (Photoshop calls it an \"angle\" gradient —your friendly ne

相关标签:
3条回答
  • 2020-12-19 17:34

    Looks to me like a job for a pixel shader. I remember seeing a Quartz Composer example that simulated a radar sweep, and that used a pixel shader to produce an effect like you're describing.

    Edit:

    Found it. This shader was written by Peter Graffignino:

    kernel vec4 radarSweep(sampler image, __color color1,__color color2, float angle, vec4 rect)
    {
        vec4 val = sample(image, samplerCoord(image));
        vec2 locCart = destCoord();
        float theta, r, frac, angleDist;
    
        locCart.x = (locCart.x - rect.z/2.0) / (rect.z/2.0);
        locCart.y = (locCart.y - rect.w/2.0) / (rect.w/2.0);
        // locCart is now normalized
        theta = degrees(atan(locCart.y, locCart.x));
        theta = (theta < 0.0) ? theta + 360.0 : theta;
        r = length(locCart);
        angleDist = theta - angle;
        angleDist = (angleDist < 0.0) ? angleDist + 360.0 : angleDist;
        frac = 1.0 - angleDist/360.0;
        // sum up 3 decaying phosphors with different time constants
        val = val*exp2(-frac/.005) + (val+.1)*exp2(-frac/.25)*color1 + val*exp2(-frac/.021)*color2;
        val = r > 1.0 ? vec4(0.0, 0.0,0.0,0.0) : val; // constrain to circle
        return val;
    }
    
    0 讨论(0)
  • 2020-12-19 17:39

    FYI: here's also a good tutorial for creating a circular progress bar using Quartz drawing.

    http://www.turnedondigital.com/blog/quartz-tutorial-how-to-draw-in-quartz/

    0 讨论(0)
  • 2020-12-19 17:51

    The thread linked above suggests using pre-made images, but this isn't an option because the colors of the gradient should be settable, the view should be resizable and the fill of the progress bar isn't always 100% full obviously (which would be the state of the gradient as shown in the picture in the thread above).

    Not a problem!

    Use the very black-to-white image from the other question (or a bigger version if you need one), in the following fashion:

    1. Clip to whatever shape you want to draw the gradient in.
    2. Fill with the color at the end of the gradient.
    3. Use the black-to-white gradient image as a mask.
    4. Fill with the color at the start of the gradient.

    You can rotate the gradient by rotating the mask image.

    This only supports the simplest case of a gradient with a color at each extreme end; it doesn't scale to three or more colors and doesn't support unusual gradient stop positioning.

    0 讨论(0)
提交回复
热议问题