Coder Social home page Coder Social logo

Comments (6)

vicc avatar vicc commented on July 16, 2024

Glad you find it useful @rjcristy! To answer your question, at the moment there isn't any straightforward way to do this. If you'd like I can add it as a feature for the 2.0 release.

If you really need this asap, there is a way. Let's begin by looking at how a linear gradient's direction works. Take the following snippet of code for example:

//Set background color for our square view
square.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleLeftToRight withFrame:square.frame andColors:@[FlatYellow, FlatRed]];

This will create a gradient that begins on the left side as FlatYellow and ends on the right as FlatRed.

square

If you right-click on "colorWithGradientStyle..." and select "Jump to Definition", you'll see that it'll take you to the method's internal implementation. Scroll down and you'll see the switch statement that contains the UIGradientStyleLeftToRight logic. Focus on the highlighted lines below. This is where the magic happens!

Since Chameleon's gradientColor is essentially created using a CAGradientLayer, it uses setStartPoint: and setEndPoint: to specify the direction.

The values for our leftToRight style are as follows:

[backgroundGradientLayer setStartPoint:CGPointMake(0.0, 0.5)];
[backgroundGradientLayer setEndPoint:CGPointMake(1.0, 0.5)];

To make it clearer how direction works take a look below:

As you can see it's pretty simple to make sense of the grid. Let's try an example. Say you wanted the FlatYellow color to begin at the lower left corner and the FlatRed color to end at the top right. All you need to do is update the values based on the coordinate grid above, in order to reflect the new startPoint and endPoint.

Now update the values to reflect your new points:

[backgroundGradientLayer setStartPoint:CGPointMake(0.0, 1.0)];
[backgroundGradientLayer setEndPoint:CGPointMake(1.0, 0.0)];

The lines above should get you the following:

You should be able to toy around with this to get the angle you want. I recommend adding a new case to the switch statement to reflect your new custom direction, unless you don't need the leftToRight direction, in which case you can just overwrite its values.

Hope this helps get you started on implementing the look you want. I know it's not the best approach, but I'll definitely explore the idea of make this easier in Chameleon 2.0 if you'd like. Maybe allow users to specify an angle as you mentioned, and handle all the math in the background. What do you think?

from chameleon.

nitrodamsel avatar nitrodamsel commented on July 16, 2024

Thank you for your very generous answer! Hope it can help others users too of this library.

from chameleon.

vicc avatar vicc commented on July 16, 2024

No worries. Good luck!

from chameleon.

gabovanlugo avatar gabovanlugo commented on July 16, 2024

Thanks for this ViccAlexander. Awesome Answer!

from chameleon.

VictorK avatar VictorK commented on July 16, 2024

Very helpful!

On Mon, Jan 25, 2016 at 9:47 PM Gabo Lugo [email protected] wrote:

Thanks for this ViccAlexander. Awesome Answer!


Reply to this email directly or view it on GitHub
https://github.com/ViccAlexander/Chameleon/issues/32#issuecomment-174788267
.

from chameleon.

bayarmanlai avatar bayarmanlai commented on July 16, 2024

Change angle still haven't applied yet in 2.1

from chameleon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.