Created
June 13, 2018 10:05
-
-
Save Ariandr/913bb02064c8821d9a227959bbffc254 to your computer and use it in GitHub Desktop.
UIView with default gradient layer and function to setup colors
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
| import UIKit | |
| class GradientLayeredView: UIView { | |
| enum GradientDirection { | |
| case leftToRight | |
| case rightToLeft | |
| case topToBottom | |
| case bottomToTop | |
| } | |
| override open class var layerClass: AnyClass { | |
| return CAGradientLayer.classForCoder() | |
| } | |
| func setupGradient(colors: [CGColor], direction: GradientDirection) { | |
| guard let layer = self.layer as? CAGradientLayer else { | |
| return | |
| } | |
| layer.colors = colors | |
| switch direction { | |
| case .leftToRight: | |
| layer.startPoint = CGPoint(x: 0.0, y: 0.5) | |
| layer.endPoint = CGPoint(x: 1.0, y: 0.5) | |
| case .rightToLeft: | |
| layer.startPoint = CGPoint(x: 1.0, y: 0.5) | |
| layer.endPoint = CGPoint(x: 0.0, y: 0.5) | |
| case .bottomToTop: | |
| layer.startPoint = CGPoint(x: 0.5, y: 1.0) | |
| layer.endPoint = CGPoint(x: 0.5, y: 0.0) | |
| case .topToBottom: | |
| layer.startPoint = CGPoint(x: 0.5, y: 0.0) | |
| layer.endPoint = CGPoint(x: 0.5, y: 1.0) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment