Last active
May 7, 2016 12:46
-
-
Save delba/412ce47a4384fd4c5eea to your computer and use it in GitHub Desktop.
Gradient background in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BackgroundView.swift | |
// Quiz | |
// | |
// Created by Damien D on 09/09/2014. | |
// Copyright (c) 2014 Damien D. All rights reserved. | |
// | |
import UIKit | |
class BackgroundView: UIView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
backgroundColor = UIColor.whiteColor() | |
} | |
required init(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func drawRect(rect: CGRect) { | |
let space: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB() | |
let colors: CFArray = [ | |
UIColor(hex: 0x085078).CGColor, | |
UIColor(hex: 0x85D8CE).CGColor | |
] | |
let locations: [CGFloat] = [0, 1] | |
let gradient = CGGradientCreateWithColors(space, colors, locations) | |
let currentContext = UIGraphicsGetCurrentContext() | |
let startPoint = CGPointZero | |
let endPoint = CGPointMake(frame.width, frame.height) | |
CGContextDrawLinearGradient(currentContext, gradient, startPoint, endPoint, 0) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIColorExtensions.swift | |
// Quiz | |
// | |
// Created by Damien D on 09/09/2014. | |
// Copyright (c) 2014 Damien D. All rights reserved. | |
// | |
import UIKit | |
extension UIColor { | |
convenience init(hex: Int, alpha: CGFloat = 1) { | |
let red = CGFloat((hex & 0xFF0000) >> 16) / 255 | |
let green = CGFloat((hex & 0xFF00) >> 8) / 255 | |
let blue = CGFloat((hex & 0xFF)) / 255 | |
self.init(red: red, green: green, blue: blue, alpha: alpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment