Skip to content

Commit

Permalink
briancorbin#13, Add Property, KnobAnchorPosition to mimic the default…
Browse files Browse the repository at this point in the history
… UISlider.
  • Loading branch information
why2pac committed Mar 3, 2017
1 parent c03ac9b commit 9f3b634
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
22 changes: 20 additions & 2 deletions SwiftRangeSlider/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ import QuartzCore
updateLabelText()
}
}

///Whether the knob is clip to bounds.
open var knobAnchorPosition: KnobAnchorPosition = .center {
didSet {
updateTrackLayerFrameAndKnobPositions()
}
}


var previousLocation = CGPoint()
var previouslySelectedKnob = Knob.Neither
Expand Down Expand Up @@ -490,11 +498,21 @@ import QuartzCore

let percentage = percentageForValue(value)

let xPosition = bounds.width * percentage
var knobDeltaX: CGFloat = 0
var knobDeltaWidth:CGFloat = 0

switch (knobAnchorPosition) {
case .inside:
knobDeltaX = (KnobSize / 2) - RangeSliderKnob.KnobDelta
knobDeltaWidth = -(KnobSize - (RangeSliderKnob.KnobDelta * 2))
case _: break
}

let xPosition = (bounds.width + knobDeltaWidth) * percentage

let yPosition = track.frame.midY

return CGPoint(x: xPosition, y: yPosition)
return CGPoint(x: xPosition + knobDeltaX, y: yPosition)
}

func percentageForValue(_ value: Double) -> CGFloat {
Expand Down
9 changes: 8 additions & 1 deletion SwiftRangeSlider/RangeSliderKnob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ enum Knob {
case Both
}

public enum KnobAnchorPosition {
case inside
case center
}

class RangeSliderKnob: CALayer {
static var KnobDelta: CGFloat = 2.0

var highlighted: Bool = false {
didSet {
if let superLayer = superlayer, highlighted {
Expand All @@ -30,7 +37,7 @@ class RangeSliderKnob: CALayer {

override func draw(in ctx: CGContext) {
if let slider = rangeSlider {
let knobFrame = bounds.insetBy(dx: 2.0, dy: 2.0)
let knobFrame = bounds.insetBy(dx: RangeSliderKnob.KnobDelta, dy: RangeSliderKnob.KnobDelta)
let cornerRadius = knobFrame.height * slider.curvaceousness / 2
let knobPath = UIBezierPath(roundedRect: knobFrame, cornerRadius: cornerRadius)

Expand Down

0 comments on commit 9f3b634

Please sign in to comment.