Monday, January 2, 2017

Gradients

Below is a simple gradient from black to dark gray. You can add many colors to the colors array.
 
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% 
Here are the default values for top to bottom:
 
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.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...