Comments (6)
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.
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.
Thank you for your very generous answer! Hope it can help others users too of this library.
from chameleon.
No worries. Good luck!
from chameleon.
Thanks for this ViccAlexander. Awesome Answer!
from chameleon.
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.
Change angle still haven't applied yet in 2.1
from chameleon.
Related Issues (20)
- Tableview scrolling is clunky when I am applying the gradient colors to the views in my tableview cell.
- Navigation Bar Hairline won't hide with a searchController in Navigation Bar
- CIEDE2000 algorithm incorrect HOT 2
- Semantic Issue - Enumeration value 'kGCColorSpaceModelXYZ' not handled in switch : UIColor+ Chameleon.m
- NSInvalidArgumentException HOT 4
- Editor placeholder in source file HOT 4
- No support for latest version of swift. HOT 6
- No gradient with tableView editActionsForRowAt
- hidesNavigationBarHairline does not work on iPhone x/xr/xs/max
- Swift 5 supporting folk? HOT 8
- Getting Complementary colors (Analogous etc.) don't return the proper colors. HOT 1
- Function produces expected type 'UIColor'; did you mean to call it with '()'? HOT 13
- Xcode 11.1 issue HOT 10
- [Question] - Color Depth / Pixel Depth
- Am getting an error while using update carthage command
- Library not loaded
- Enumeration value 'kCGColorSpaceModelXYZ' not handled in switch
- Gradient starts giving rare hex values
- Building not working on Xcode 12
- Build fails with message "Function produces expected type 'UIColor()'; did you mean to call it with '()'?" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chameleon.