Skip to content

Instantly share code, notes, and snippets.

@GeekTree0101
Created July 18, 2018 14:03
Show Gist options
  • Save GeekTree0101/412c06dbf5cb93c0d21cb8d94029f736 to your computer and use it in GitHub Desktop.
Save GeekTree0101/412c06dbf5cb93c0d21cb8d94029f736 to your computer and use it in GitHub Desktop.
Texture GradientLayout Example
class mothorNode: ASDisplayNode {
let childNode = ASDisplayNode()
// ....omit.....
override func layoutDidFinish() {
super.layoutDidFinish()
self.childNode
.gradientBackgroundColor([UIColor.red.cgColor, UIColor.white.cgColor],
direction: .horizontal)
}
// ... omit....
}
extension ASDisplayNode {
enum GradientDirection {
case horizontal
case vertical
case adjust(CGPoint, CGPoint)
var points: (start: CGPoint, end: CGPoint) {
switch self {
case .horizontal:
return (start: .init(x: 0.0, y: 0.5), end: .init(x: 1.0, y: 0.5))
case .vertical:
return (start: .init(x: 0.5, y: 0.0), end: .init(x: 0.5, y: 1.0))
case .adjust(let start, let end):
return (start: start, end: end)
}
}
}
func gradientBackgroundColor(_ colors: [CGColor], direction: GradientDirection) {
DispatchQueue.main.async {
self.backgroundColor = .clear
self.clipsToBounds = true
let gradient = CAGradientLayer()
gradient.frame = .init(origin: .zero, size: self.calculatedSize)
gradient.colors = colors
gradient.startPoint = direction.points.start
gradient.endPoint = direction.points.end
self.layer.insertSublayer(gradient, at: 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment