Created
April 26, 2019 08:45
-
-
Save GeekTree0101/f2105ef755ca3b119312c88f7739fb55 to your computer and use it in GitHub Desktop.
UISlider + Texture
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 DGSliderView: UISlider { | |
override func trackRect(forBounds bounds: CGRect) -> CGRect { | |
return CGRect.init(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: 10.0) | |
} | |
} | |
class SliderNode: ASDisplayNode { | |
struct Const { | |
static let height: ASDimension = .init(unit: .points, value: 10.0) | |
} | |
let sliderNode = ASDisplayNode.init(viewBlock: { () -> UIView in | |
let view = DGSliderView.init() | |
view.tintColor = .orange | |
view.backgroundColor = .clear | |
view.maximumTrackTintColor = .clear | |
return view | |
}) | |
let backgroundNodes: [ASDisplayNode] = (0 ..< 3).map({ _ -> ASDisplayNode in | |
let node = ASDisplayNode() | |
node.backgroundColor = .gray | |
node.style.height = Const.height | |
return node | |
}) | |
var sliderView: UISlider? { | |
return sliderNode.view as? UISlider | |
} | |
override init() { | |
super.init() | |
self.automaticallyManagesSubnodes = true | |
} | |
override func layout() { | |
super.layout() | |
} | |
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { | |
for e in self.backgroundNodes { | |
e.style.flexGrow = 1.0 | |
} | |
let backgroundLayout = ASStackLayoutSpec(direction: .horizontal, | |
spacing: 2.0, | |
justifyContent: .start, | |
alignItems: .stretch, | |
children: self.backgroundNodes) | |
return ASOverlayLayoutSpec(child: backgroundLayout, overlay: sliderNode) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment