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
    }
}
 
23
Kudos
 
23
Kudos

Now read this

The Road To 1000 Downloads

Today is a day of celebration, the cumulative downloads of my 3 iOS games broke the one thousand mark arriving at 1005 downloads! It is exactly 153 days since the release of the first game. I don’t know how representative this is of an... Continue →