Skip to content

Commit 30fe0ae

Browse files
committed
[Slider] Fix layout and sizing
Fixes #130 (tested using a 2 second video) Additional changes: - Added and modified some mark comments for better structure. - Added 8 pt margins around the playback slider in default config to visually compensate for Slider layout code changes.
1 parent 8b7b33a commit 30fe0ae

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

MobilePlayer/Views/MobilePlayerControlsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class MobilePlayerControlsView: UIView {
4646
if bottomBar.elements.count == 0 {
4747
bottomBar.addElementUsingConfig(ToggleButtonConfig(dictionary: ["type": "toggleButton", "identifier": "play"]))
4848
bottomBar.addElementUsingConfig(LabelConfig(dictionary: ["type": "label", "identifier": "currentTime"]))
49-
bottomBar.addElementUsingConfig(SliderConfig(dictionary: ["type": "slider", "identifier": "playback"]))
49+
bottomBar.addElementUsingConfig(SliderConfig(dictionary: ["type": "slider", "identifier": "playback", "marginLeft": 8, "marginRight": 8]))
5050
bottomBar.addElementUsingConfig(LabelConfig(dictionary: ["type": "label", "identifier": "duration", "marginRight": 8]))
5151
}
5252
addSubview(bottomBar)

MobilePlayer/Views/Slider.swift

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
import UIKit
1010

11+
// MARK: - Delegate
1112
protocol SliderDelegate: class {
1213
func sliderThumbPanDidBegin(slider: Slider)
1314
func sliderThumbDidPan(slider: Slider)
1415
func sliderThumbPanDidEnd(slider: Slider)
1516
}
1617

18+
// MARK: - Class
1719
class 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

Comments
 (0)