Skip to content

Instantly share code, notes, and snippets.

@Ariandr
Created June 13, 2018 10:05
Show Gist options
  • Select an option

  • Save Ariandr/913bb02064c8821d9a227959bbffc254 to your computer and use it in GitHub Desktop.

Select an option

Save Ariandr/913bb02064c8821d9a227959bbffc254 to your computer and use it in GitHub Desktop.
UIView with default gradient layer and function to setup colors
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