Skip to content

Instantly share code, notes, and snippets.

@Coder-ACJHP
Created February 5, 2019 00:01
Show Gist options
  • Save Coder-ACJHP/62d97ef557b8774ffd2814a4c0223b34 to your computer and use it in GitHub Desktop.
Save Coder-ACJHP/62d97ef557b8774ffd2814a4c0223b34 to your computer and use it in GitHub Desktop.
Designable from storyboard, reusable vertical slider
import UIKit
protocol VerticalSliderDelegate: class {
func sliderValueIsChanged(_ verticalSlider: CCVerticalSlider, value: CGFloat)
}
@IBDesignable open class CCVerticalSlider: UIView {
let shapeLayer = CAShapeLayer()
var delegate: VerticalSliderDelegate?
internal let slider = CCustomSlider()
// required for IBDesignable class to properly render
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
// required for IBDesignable class to properly render
required override public init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
fileprivate func initialize() {
updateSlider()
addSubview(slider)
}
fileprivate func updateSlider() {
if !ascending {
slider.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi) * -0.5)
} else {
slider.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi) * 0.5).scaledBy(x: 1, y: -1)
}
slider.minimumValue = minimumValue
slider.maximumValue = maximumValue
slider.value = value
slider.thumbTintColor = thumbTintColor
slider.minimumTrackTintColor = minimumTrackTintColor
slider.maximumTrackTintColor = maximumTrackTintColor
slider.isContinuous = isContinuous
slider.trackWidth = self.trackWidth
slider.layer.cornerRadius = self.cornersRadius
let rectangle = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
let roundedRectPath = UIBezierPath(roundedRect: rectangle, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: self.cornersRadius, height: self.cornersRadius))
shapeLayer.path = roundedRectPath.cgPath
shapeLayer.strokeColor = self.borderColor?.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineCap = .round
shapeLayer.lineJoin = .round
shapeLayer.lineWidth = 0.3
self.layer.insertSublayer(shapeLayer, below: slider.layer)
self.layer.cornerRadius = self.cornersRadius
slider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
}
@IBInspectable open var ascending: Bool = false {
didSet {
updateSlider()
}
}
override open func layoutSubviews() {
super.layoutSubviews()
slider.bounds.size.width = bounds.height - 6
slider.bounds.size.height = bounds.width
slider.center.x = self.bounds.midX + 3
slider.center.y = self.bounds.midY
}
override open var intrinsicContentSize: CGSize {
get {
return CGSize(width: slider.intrinsicContentSize.height, height: slider.intrinsicContentSize.width)
}
}
@IBInspectable open var minimumValue: Float = -1 {
didSet {
updateSlider()
}
}
@IBInspectable open var maximumValue: Float = 1 {
didSet {
updateSlider()
}
}
@IBInspectable open var value: Float {
get {
return slider.value
}
set {
slider.setValue(newValue, animated: true)
}
}
@IBInspectable open var thumbTintColor: UIColor? {
didSet {
updateSlider()
}
}
@IBInspectable open var minimumTrackTintColor: UIColor? {
didSet {
updateSlider()
}
}
@IBInspectable open var maximumTrackTintColor: UIColor? {
didSet {
updateSlider()
}
}
@IBInspectable open var borderColor: UIColor? {
didSet {
updateSlider()
}
}
@IBInspectable open var trackWidth: CGFloat = 10 {
didSet {
updateSlider()
}
}
@IBInspectable open var cornersRadius: CGFloat = 5 {
didSet {
updateSlider()
}
}
@IBInspectable open var isContinuous: Bool = true {
didSet {
updateSlider()
}
}
@objc fileprivate func sliderValueChanged(_ sender: UISlider) {
delegate?.sliderValueIsChanged(self, value: CGFloat(sender.value))
}
}
class CCustomSlider: UISlider {
@IBInspectable var trackWidth: CGFloat = 20
override func trackRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: trackWidth))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment