Skip to content

Instantly share code, notes, and snippets.

@bleft
Last active January 19, 2018 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bleft/df58270b76fe444e08952d0c7fab40af to your computer and use it in GitHub Desktop.
Save bleft/df58270b76fe444e08952d0c7fab40af to your computer and use it in GitHub Desktop.
UIView with CAGradient
class MyView: UIView {
private let gradient = CAGradientLayer()
override func draw(_ rect: CGRect) {
super.draw(rect)
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor] // beliebig viele Farben für den Verlauf einstellen
gradient.startPoint = CGPoint(x: 0, y: 0) // über start und endPoint kann die Richtung des Verlaufes verändert werden.
gradient.endPoint = CGPoint(x: 0, y: 1)
gradient.frame = self.bounds // wichtig: Der Wert muss nach dem Layout gesetzt werden
gradient.removeFromSuperlayer()
self.layer.insertSublayer(gradient, at: 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment