Last active
January 19, 2018 13:09
-
-
Save bleft/df58270b76fe444e08952d0c7fab40af to your computer and use it in GitHub Desktop.
UIView with CAGradient
This file contains hidden or 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
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