Skip to content

Instantly share code, notes, and snippets.

@psugihara
Created November 25, 2014 08:05
Show Gist options
  • Save psugihara/97deee1679e9a4beb4bf to your computer and use it in GitHub Desktop.
Save psugihara/97deee1679e9a4beb4bf to your computer and use it in GitHub Desktop.
UIView with gradient background in Swift
class OLGradientView: UIView {
override class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initGradientLayer()
}
override init(frame: CGRect) {
super.init(frame: frame)
initGradientLayer()
}
func initGradientLayer() {
let middleColor = UIColor(red: 0.243, green: 0.251, blue: 0.259, alpha: 1)
let outerColor = UIColor(red: 0.098, green: 0.122, blue: 0.133, alpha: 1)
let gradientColors: [CGColor] = [outerColor.CGColor, middleColor.CGColor, outerColor.CGColor]
let gradientLocations: [Float] = [0.0, 0.53, 0.93]
let gradientLayer: CAGradientLayer = self.layer as CAGradientLayer
gradientLayer.colors = gradientColors
gradientLayer.locations = gradientLocations
println(self.layer.backgroundColor)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment