Created
April 14, 2015 18:25
-
-
Save DuncanMC/c41babda5709b9d41ca8 to your computer and use it in GitHub Desktop.
A playground to create a UIImageView with a gradient on top of it
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
import UIKit | |
import AVFoundation | |
class ImageViewWithGradient: UIImageView | |
{ | |
let myGradientLayer: CAGradientLayer | |
override init(frame: CGRect) | |
{ | |
myGradientLayer = CAGradientLayer() | |
super.init(frame: frame) | |
self.setup() | |
} | |
required init(coder aDecoder: NSCoder) | |
{ | |
myGradientLayer = CAGradientLayer() | |
super.init(coder: aDecoder) | |
self.setup() | |
} | |
func setup() | |
{ | |
myGradientLayer.startPoint = CGPoint(x: 0, y: 0) | |
myGradientLayer.endPoint = CGPoint(x: 1, y: 1) | |
let colors: [CGColorRef] = [ | |
UIColor.clearColor().CGColor, | |
UIColor(red: 0, green: 0, blue: 0, alpha: 0.3).CGColor, | |
UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).CGColor, | |
UIColor(red: 0, green: 0, blue: 0, alpha: 0.3).CGColor, | |
UIColor.clearColor().CGColor ] | |
myGradientLayer.colors = colors | |
myGradientLayer.opaque = false | |
myGradientLayer.locations = [0.0, 0.3, 0.5, 0.7, 1.0] | |
self.layer.addSublayer(myGradientLayer) | |
} | |
override func layoutSubviews() | |
{ | |
myGradientLayer.frame = self.layer.bounds | |
} | |
} | |
var aView = ImageViewWithGradient(frame: CGRect(x: 0, y: 0, width: 500, height: 500)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment