Skip to content

Commit

Permalink
Fixed issue #6
Browse files Browse the repository at this point in the history
Fixed by updating layer frames in layoutSublayersOfLayer() instead of
initializeLayers()
  • Loading branch information
William Archimède committed Mar 3, 2016
1 parent 25cc45e commit 937106d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
14 changes: 12 additions & 2 deletions RangeSlider/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9060" systemVersion="14F1021" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="14F1605" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9051"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand Down Expand Up @@ -50,10 +50,20 @@
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="AsD-zw-Yxv" customClass="RangeSlider" customModule="RangeSlider" customModuleProvider="target">
<rect key="frame" x="50" y="449" width="500" height="30"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="bqb-l3-6NV"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="kQO-Yc-D8F" firstAttribute="top" secondItem="AsD-zw-Yxv" secondAttribute="bottom" constant="20" id="345-Gp-Xga"/>
<constraint firstItem="kQO-Yc-D8F" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="H1I-SU-ViG"/>
<constraint firstAttribute="trailing" secondItem="AsD-zw-Yxv" secondAttribute="trailing" constant="50" id="M26-Ns-STa"/>
<constraint firstItem="AsD-zw-Yxv" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" constant="50" id="ZSF-Ee-JQM"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="kQO-Yc-D8F" secondAttribute="bottom" constant="58" id="bL8-88-isy"/>
</constraints>
</view>
Expand Down
85 changes: 47 additions & 38 deletions RangeSlider/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ class RangeSliderTrackLayer: CALayer {
weak var rangeSlider: RangeSlider?

override func drawInContext(ctx: CGContext) {
if let slider = rangeSlider {
// Clip
let cornerRadius = bounds.height * slider.curvaceousness / 2.0
let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
CGContextAddPath(ctx, path.CGPath)
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 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))
let upperValuePosition = CGFloat(slider.positionForValue(slider.upperValue))
let rect = CGRect(x: lowerValuePosition, y: 0.0, width: upperValuePosition - lowerValuePosition, height: bounds.height)
CGContextFillRect(ctx, rect)
}
// Fill the highlighted range
CGContextSetFillColorWithColor(ctx, slider.trackHighlightTintColor.CGColor)
let lowerValuePosition = CGFloat(slider.positionForValue(slider.lowerValue))
let upperValuePosition = CGFloat(slider.positionForValue(slider.upperValue))
let rect = CGRect(x: lowerValuePosition, y: 0.0, width: upperValuePosition - lowerValuePosition, height: bounds.height)
CGContextFillRect(ctx, rect)
}
}

Expand All @@ -43,28 +45,30 @@ class RangeSliderThumbLayer: CALayer {
weak var rangeSlider: RangeSlider?

override func drawInContext(ctx: CGContext) {
if let slider = rangeSlider {
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)
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)
// 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)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextStrokePath(ctx)
// Outline
let strokeColor = UIColor.grayColor()
CGContextSetStrokeColorWithColor(ctx, strokeColor.CGColor)
CGContextSetLineWidth(ctx, 0.5)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextStrokePath(ctx)

if highlighted {
CGContextSetFillColorWithColor(ctx, UIColor(white: 0.0, alpha: 0.1).CGColor)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextFillPath(ctx)
}
if highlighted {
CGContextSetFillColorWithColor(ctx, UIColor(white: 0.0, alpha: 0.1).CGColor)
CGContextAddPath(ctx, thumbPath.CGPath)
CGContextFillPath(ctx)
}
}
}
Expand Down Expand Up @@ -174,7 +178,14 @@ class RangeSlider: UIControl {
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)
Expand All @@ -186,8 +197,6 @@ class RangeSlider: UIControl {
upperThumbLayer.rangeSlider = self
upperThumbLayer.contentsScale = UIScreen.mainScreen().scale
layer.addSublayer(upperThumbLayer)

updateLayerFrames()
}

func updateLayerFrames() {
Expand All @@ -210,7 +219,7 @@ class RangeSlider: UIControl {

func positionForValue(value: Double) -> Double {
return Double(bounds.width - thumbWidth) * (value - minimumValue) /
(maximumValue - minimumValue) + Double(thumbWidth / 2.0)
(maximumValue - minimumValue) + Double(thumbWidth/2.0)
}

func boundValue(value: Double, toLowerValue lowerValue: Double, upperValue: Double) -> Double {
Expand Down

0 comments on commit 937106d

Please sign in to comment.