Programmatic Background Gradient Layer

I saw an interesting post about gradient programmatically that was written in Objective-C: https://danielbeard.wordpress.com/2012/02/25/gradient-background-for-uiview-in-ios/

I ported to Swift for my own use and it seems to work fine still!

import Foundation
import QuartzCore
import SpriteKit

class BackgroundLayer
{
    var greyGradient = CAGradientLayer()
    var blueGradient = CAGradientLayer()

    init()
    {
        var colorOne = UIColor(white: 0.9, alpha: 1.0)
        var colorTwo = UIColor(hue: 0.625, saturation: 0.0, brightness: 0.85, alpha: 1.0)
        var colorThree = UIColor(hue: 0.625, saturation: 0.0, brightness: 0.7, alpha: 1.0)
        var colorFour = UIColor(hue: 0.625, saturation: 0.0, brightness: 0.4, alpha: 1.0)
        var colors = [CGColor](arrayLiteral: colorOne.CGColor, colorTwo.CGColor, colorThree.CGColor, colorFour.CGColor)
        println(colors.count)

        var locations = [CGFloat](arrayLiteral: 0.0, 0.02, 0.99, 1.0)

        greyGradient.colors = colors;
        greyGradient.locations = locations;

        var blueColors = [UIColor]()
        blueColors.append(UIColor(red: 120/255.0, green: 135/255.0, blue: 150/255.0, alpha: 1.0))
        blueColors.append(UIColor(red: 57/255.0, green: 79/255.0, blue: 96/255.0, alpha: 1.0))
        var bColors = [CGColor]()
        for color in blueColors
        {
            bColors.append(color.CGColor)
        }
        var bStoppers = [CGFloat](arrayLiteral: 1.0, 1.0)
        blueGradient.colors = bColors
        blueGradient.locations = bStoppers
    }
}
 
26
Kudos
 
26
Kudos

Now read this

Calculating the Collatz conjecture with Swift

The Collatz conjecture is a mathematical conjecture named after Lothar Collatz. The basic idea is described in Wikipedia as the following: “Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by... Continue →