Skip to content

Commit

Permalink
Added thumbBorderColor and thumbBorderWidth properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkoshy7 committed Aug 16, 2016
1 parent 9c66db3 commit 5e69c2b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The range slider can be customized and information can be accessed through these
+ `trackTintColor` : The track color
+ `trackHighlightTintColor` : The color of the section of the track located between the two thumbs
+ `thumbTintColor`: The thumb color
+ `thumbBorderColor`: The thumb border color
+ `thumbBorderWidth`: The width of the thumb border
+ `curvaceousness` : From 0.0 for square thumbs to 1.0 for circle thumbs

## License
Expand Down
65 changes: 44 additions & 21 deletions RangeSlider/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class RangeSliderTrackLayer: CALayer {
guard let slider = rangeSlider else {
return
}

// Clip
let cornerRadius = bounds.height * slider.curvaceousness / 2.0
let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
CGContextAddPath(ctx, path.CGPath)

// Fill the track
CGContextSetFillColorWithColor(ctx, slider.trackTintColor.CGColor)
CGContextAddPath(ctx, path.CGPath)
CGContextFillPath(ctx)

// Fill the highlighted range
CGContextSetFillColorWithColor(ctx, slider.trackHighlightTintColor.CGColor)
let lowerValuePosition = CGFloat(slider.positionForValue(slider.lowerValue))
Expand All @@ -37,34 +37,45 @@ class RangeSliderTrackLayer: CALayer {
}

class RangeSliderThumbLayer: CALayer {

var highlighted: Bool = false {
didSet {
setNeedsDisplay()
}
}
weak var rangeSlider: RangeSlider?

var strokeColor: UIColor = UIColor.grayColor() {
didSet {
setNeedsDisplay()
}
}
var lineWidth: CGFloat = 0.5 {
didSet {
setNeedsDisplay()
}
}

override func drawInContext(ctx: CGContext) {
guard let slider = rangeSlider else {
return
}

let thumbFrame = bounds.insetBy(dx: 2.0, dy: 2.0)
let cornerRadius = thumbFrame.height * slider.curvaceousness / 2.0
let thumbPath = UIBezierPath(roundedRect: thumbFrame, cornerRadius: cornerRadius)

// Fill
CGContextSetFillColorWithColor(ctx, slider.thumbTintColor.CGColor)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextFillPath(ctx)

// Outline
let strokeColor = UIColor.grayColor()
CGContextSetStrokeColorWithColor(ctx, strokeColor.CGColor)
CGContextSetLineWidth(ctx, 0.5)
CGContextSetLineWidth(ctx, lineWidth)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextStrokePath(ctx)

if highlighted {
CGContextSetFillColorWithColor(ctx, UIColor(white: 0.0, alpha: 0.1).CGColor)
CGContextAddPath(ctx, thumbPath.CGPath)
Expand Down Expand Up @@ -115,25 +126,39 @@ class RangeSlider: UIControl {
return Double(thumbWidth)*(maximumValue - minimumValue) / Double(bounds.width)
}

@IBInspectable var trackTintColor = UIColor(white: 0.9, alpha: 1.0) {
@IBInspectable var trackTintColor: UIColor = UIColor(white: 0.9, alpha: 1.0) {
didSet {
trackLayer.setNeedsDisplay()
}
}

@IBInspectable var trackHighlightTintColor = UIColor(red: 0.0, green: 0.45, blue: 0.94, alpha: 1.0) {
@IBInspectable var trackHighlightTintColor: UIColor = UIColor(red: 0.0, green: 0.45, blue: 0.94, alpha: 1.0) {
didSet {
trackLayer.setNeedsDisplay()
}
}

@IBInspectable var thumbTintColor = UIColor.whiteColor() {
@IBInspectable var thumbTintColor: UIColor = UIColor.whiteColor() {
didSet {
lowerThumbLayer.setNeedsDisplay()
upperThumbLayer.setNeedsDisplay()
}
}

@IBInspectable var thumbBorderColor: UIColor = UIColor.grayColor() {
didSet {
lowerThumbLayer.strokeColor = thumbBorderColor
upperThumbLayer.strokeColor = thumbBorderColor
}
}

@IBInspectable var thumbBorderWidth: CGFloat = 0.5 {
didSet {
lowerThumbLayer.lineWidth = thumbBorderWidth
upperThumbLayer.lineWidth = thumbBorderWidth
}
}

@IBInspectable var curvaceousness: CGFloat = 1.0 {
didSet {
if curvaceousness < 0.0 {
Expand Down Expand Up @@ -168,32 +193,30 @@ class RangeSlider: UIControl {

override init(frame: CGRect) {
super.init(frame: frame)

initializeLayers()
}

required init?(coder: NSCoder) {
super.init(coder: coder)

initializeLayers()
}

override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
updateLayerFrames()
}

private func initializeLayers() {
layer.backgroundColor = UIColor.clearColor().CGColor

trackLayer.rangeSlider = self
trackLayer.contentsScale = UIScreen.mainScreen().scale
layer.addSublayer(trackLayer)

lowerThumbLayer.rangeSlider = self
lowerThumbLayer.contentsScale = UIScreen.mainScreen().scale
layer.addSublayer(lowerThumbLayer)

upperThumbLayer.rangeSlider = self
upperThumbLayer.contentsScale = UIScreen.mainScreen().scale
layer.addSublayer(upperThumbLayer)
Expand Down Expand Up @@ -225,8 +248,8 @@ class RangeSlider: UIControl {
func boundValue(value: Double, toLowerValue lowerValue: Double, upperValue: Double) -> Double {
return min(max(value, lowerValue), upperValue)
}


// MARK: - Touches

override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
Expand Down

0 comments on commit 5e69c2b

Please sign in to comment.