Below is a simple gradient from black to dark gray. You can add many colors to the colors array.
You can manually set the startPoint and endPoint properties for the CAGradientLayer to change the default gradient direction from top to bottom.
These are of a type CGPoint which is basically just and x and y value. BUT, the max value for x or y is only 1.
You have to think of the values as a percentages, like:
If you wanted to make it HORIZONTAL, then you could start from upper left to upper right.
It would look like this:
Now, can you guess what a 45 degree angle would look like?
It would look like this:
Here's a diagram which might clear up how these CGPoints work:
(Swift 3)
let newLayer = CAGradientLayer() newLayer.colors = [UIColor.black.cgColor, UIColor.darkGray.cgColor] newLayer.frame = view.frame view.layers.insertSublayer(newLayer, at: 0)
You can manually set the startPoint and endPoint properties for the CAGradientLayer to change the default gradient direction from top to bottom.
These are of a type CGPoint which is basically just and x and y value. BUT, the max value for x or y is only 1.
You have to think of the values as a percentages, like:
- 0 = 0%
- .25 = 25%
- .5 = 50%
- .75 = 75%
- 1 = 100%
newLayer.startPoint = CGPoint(x: 0, y: 0) // Upper left corner
newLayer.endPoint = CGPoint(x: 0, y: 1) // Lower left corner
If you wanted to make it HORIZONTAL, then you could start from upper left to upper right.
It would look like this:
newLayer.startPoint = CGPoint(x: 0, y: 0) // Upper left corner
newLayer.endPoint = CGPoint(x: 1, y: 0) // Upper right corner
Now, can you guess what a 45 degree angle would look like?
It would look like this:
newLayer.startPoint = CGPoint(x: 0, y: 0) // Upper left corner
newLayer.endPoint = CGPoint(x: 1, y: 1) // Lower right corner
Here's a diagram which might clear up how these CGPoints work:
CGPoint X,Y Scale in iOS |
(Swift 3)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.