Kristofers
Kristofers

Reputation: 13

How can i change the degree at which the gradient starts

i'm using this repository to draw a gradient,

https://github.com/paiv/AngleGradientLayer/blob/master/AngleGradient/AngleGradientLayer.m

I'm trying to get the angle at which the gradients start, to change.

The gradient on the left is what i have currently, and on the right what I'm trying to achieve.

http://i.imgur.com/muUPD0U.png

I need to manage to edit the code, I got the result i wanted on the right by making the layer bigger and rotating it but it means drawing more pixels on screen, which is not efficient.

Thanks in advance.

AngleGradientLayer *l = (AngleGradientLayer *)self.layer;

l.colors = [NSArray arrayWithObjects:
            (id)[UIColor colorWithRed:38.0 / 255.0 green:154.0 / 255.0 blue:151.0 / 255.0 alpha:1].CGColor,
            (id)[UIColor colorWithRed:231.0 / 255.0 green:210.0 / 255.0 blue:130.0 / 255.0 alpha:1].CGColor,
            (id)[UIColor colorWithRed:195.0 / 255.0 green:85.0 / 255.0 blue:82.0 / 255.0 alpha:1].CGColor,
            nil];


NSMutableArray *locations = [[NSMutableArray alloc] initWithCapacity:3];

self.value1 = [NSNumber numberWithFloat:(0.0)];
self.value2 = [NSNumber numberWithFloat:(0.5)];
self.value3 = [NSNumber numberWithFloat:(1.0)];

[locations addObject: self.value1];
[locations addObject: self.value2];
[locations addObject: self.value3];

l.locations = locations;

Upvotes: 0

Views: 257

Answers (1)

Eiko
Eiko

Reputation: 25632

Here is some code I've patched into the angleGradient(...) function in AngleGradientLayer.m.

float offset = M_PI / 4;
float angle = atan2f(dirY, dirX) + offset;
while (angle < 0) angle += 2 * M_PI;
while (angle > 2 * M_PI) angle -= 2 * M_PI;
while (angle < 0) angle += 2 * M_PI;

It replaces these lines

    float angle = atan2f(dirY, dirX);
    if (dirY < 0) angle += 2 * M_PI;

Typically, you want to make offset a parameter of the function and a property of AngleGradientLayer. offset is in radiants (0 ... 2π). You can go with -offset if you want to shift following counter-clockwise logic.

Upvotes: 0

Related Questions