88
99import UIKit
1010
11+ // MARK: - Delegate
1112protocol SliderDelegate : class {
1213 func sliderThumbPanDidBegin( slider: Slider )
1314 func sliderThumbDidPan( slider: Slider )
1415 func sliderThumbPanDidEnd( slider: Slider )
1516}
1617
18+ // MARK: - Class
1719class Slider : UIView {
1820 let config : SliderConfig
1921 weak var delegate : SliderDelegate ?
@@ -27,7 +29,7 @@ class Slider: UIView {
2729 let minimumTrack = UIView ( frame: CGRectZero)
2830 let thumb = UIView ( frame: CGRectZero)
2931
30- // MARK: - Initialization
32+ // MARK: Initialization
3133
3234 init ( config: SliderConfig = SliderConfig ( ) ) {
3335 self . config = config
@@ -57,7 +59,7 @@ class Slider: UIView {
5759 fatalError ( " init(coder:) has not been implemented " )
5860 }
5961
60- // MARK: - Setters
62+ // MARK: Setters
6163
6264 func setValue( value: Float , animatedForDuration duration: NSTimeInterval ) {
6365 self . value = value
@@ -84,7 +86,7 @@ class Slider: UIView {
8486 }
8587 }
8688
87- // MARK: - Seeking
89+ // MARK: Seeking
8890
8991 func didPanThumb( recognizer: UIPanGestureRecognizer ! ) {
9092 let locationInTrack = recognizer. locationInView ( maximumTrack)
@@ -108,32 +110,45 @@ class Slider: UIView {
108110 }
109111 }
110112
111- // MARK: - Layout
113+ // MARK: Layout
112114
113115 override func sizeThatFits( size: CGSize ) -> CGSize {
114- let biggestHeight = config. thumbHeight > config. trackHeight ? config. thumbHeight : config. trackHeight
115- let width = ( config. widthCalculation == . AsDefined) ? config. width : config. thumbWidth * 2
116- let height = size. height < biggestHeight ? biggestHeight : size. height
116+ let width = ( config. widthCalculation == . AsDefined) ? config. width : size. width
117+
118+ let minHeight = max ( config. thumbHeight, config. trackHeight)
119+ let height = ( size. height < minHeight) ? minHeight : size. height
120+
117121 return CGSize ( width: width, height: height)
118122 }
119123
120124 override func layoutSubviews( ) {
121- let size = bounds. size
125+ let realMaximumValue = max ( 0.00001 , CGFloat ( maximumValue - minimumValue) )
126+ let realAvailableValue = max ( 0 , min ( realMaximumValue, CGFloat ( availableValue - minimumValue) ) )
127+ let realValue = max ( 0 , min ( realMaximumValue, CGFloat ( value - minimumValue) ) )
128+
122129 maximumTrack. frame = CGRect (
123- x: config. thumbWidth / 2 ,
124- y: ( size. height - config. trackHeight) / 2 ,
125- width: size. width - config. thumbWidth,
130+ x: 0 ,
131+ y: ( bounds. height - config. trackHeight) / 2 ,
132+ width: bounds. width,
133+ height: config. trackHeight)
134+
135+ availableTrack. frame = CGRect (
136+ x: 0 ,
137+ y: 0 ,
138+ width: maximumTrack. frame. width * ( realAvailableValue / realMaximumValue) ,
126139 height: config. trackHeight)
127- let realMaximumValue = maximumValue - minimumValue
128- let minimumTrackWidth = realMaximumValue != 0 ? maximumTrack. frame. size. width * CGFloat( ( value - minimumValue) / realMaximumValue) : 0
129- minimumTrack. frame = CGRect ( x: 0 , y: 0 , width: minimumTrackWidth, height: config. trackHeight)
130- let availableTrackWidth = realMaximumValue != 0 ? maximumTrack. frame. size. width * CGFloat( ( availableValue - minimumValue) / realMaximumValue) : 0
131- availableTrack. frame = CGRect ( x: 0 , y: 0 , width: availableTrackWidth, height: config. trackHeight)
140+
132141 thumb. frame = CGRect (
133- x: minimumTrackWidth ,
134- y: ( size . height - config. thumbHeight) / 2 ,
142+ x: ( bounds . width - config . thumbWidth ) * ( realValue / realMaximumValue ) ,
143+ y: ( bounds . height - config. thumbHeight) / 2 ,
135144 width: config. thumbWidth,
136145 height: config. thumbHeight)
146+
147+ minimumTrack. frame = CGRect (
148+ x: 0 ,
149+ y: 0 ,
150+ width: thumb. frame. midX,
151+ height: config. trackHeight)
137152 }
138153}
139154
0 commit comments