|
| 1 | +import AutoLayout |
| 2 | +import UIKit |
| 3 | + |
| 4 | +/// A UIKit component that displays a slider. |
| 5 | +open class UKSlider: UIView, UKComponent { |
| 6 | + // MARK: - Properties |
| 7 | + |
| 8 | + /// A closure that is triggered when the `currentValue` changes. |
| 9 | + public var onValueChange: (CGFloat) -> Void |
| 10 | + |
| 11 | + /// A model that defines the appearance properties. |
| 12 | + public var model: SliderVM { |
| 13 | + didSet { |
| 14 | + self.update(oldValue) |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + /// The current value of the slider. |
| 19 | + public var currentValue: CGFloat { |
| 20 | + didSet { |
| 21 | + guard self.currentValue != oldValue else { return } |
| 22 | + self.updateSliderAppearance() |
| 23 | + self.onValueChange(self.currentValue) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // MARK: - Subviews |
| 28 | + |
| 29 | + /// The background view of the slider track. |
| 30 | + public let backgroundView = UIView() |
| 31 | + |
| 32 | + /// The filled portion of the slider track. |
| 33 | + public let barView = UIView() |
| 34 | + |
| 35 | + /// A shape layer used to render striped styling. |
| 36 | + public let stripedLayer = CAShapeLayer() |
| 37 | + |
| 38 | + /// The draggable handle representing the current value. |
| 39 | + public let handleView = UIView() |
| 40 | + |
| 41 | + /// An overlay view for handle for the `large` style. |
| 42 | + private let handleOverlayView = UIView() |
| 43 | + |
| 44 | + // MARK: - Layout Constraints |
| 45 | + |
| 46 | + private var barViewConstraints = LayoutConstraints() |
| 47 | + private var backgroundViewConstraints = LayoutConstraints() |
| 48 | + private var handleViewConstraints = LayoutConstraints() |
| 49 | + |
| 50 | + // MARK: - Private Properties |
| 51 | + |
| 52 | + private var isDragging = false |
| 53 | + |
| 54 | + private var progress: CGFloat { |
| 55 | + self.model.progress(for: self.currentValue) |
| 56 | + } |
| 57 | + |
| 58 | + // MARK: - UIView Properties |
| 59 | + |
| 60 | + open override var intrinsicContentSize: CGSize { |
| 61 | + return self.sizeThatFits(UIView.layoutFittingExpandedSize) |
| 62 | + } |
| 63 | + |
| 64 | + // MARK: - Initialization |
| 65 | + |
| 66 | + /// Initializer. |
| 67 | + /// - Parameters: |
| 68 | + /// - initialValue: The initial slider value. Defaults to `0`. |
| 69 | + /// - model: A model that defines the appearance properties. |
| 70 | + /// - onValueChange: A closure triggered whenever `currentValue` changes. |
| 71 | + public init( |
| 72 | + initialValue: CGFloat = 0, |
| 73 | + model: SliderVM = .init(), |
| 74 | + onValueChange: @escaping (CGFloat) -> Void = { _ in } |
| 75 | + ) { |
| 76 | + self.currentValue = initialValue |
| 77 | + self.model = model |
| 78 | + self.onValueChange = onValueChange |
| 79 | + super.init(frame: .zero) |
| 80 | + |
| 81 | + self.setup() |
| 82 | + self.style() |
| 83 | + self.layout() |
| 84 | + } |
| 85 | + |
| 86 | + public required init?(coder: NSCoder) { |
| 87 | + fatalError("init(coder:) has not been implemented") |
| 88 | + } |
| 89 | + |
| 90 | + // MARK: - Setup |
| 91 | + |
| 92 | + private func setup() { |
| 93 | + self.addSubview(self.backgroundView) |
| 94 | + self.addSubview(self.barView) |
| 95 | + self.addSubview(self.handleView) |
| 96 | + self.backgroundView.layer.addSublayer(self.stripedLayer) |
| 97 | + self.handleView.addSubview(self.handleOverlayView) |
| 98 | + } |
| 99 | + |
| 100 | + // MARK: - Style |
| 101 | + |
| 102 | + private func style() { |
| 103 | + Self.Style.backgroundView(self.backgroundView, model: self.model) |
| 104 | + Self.Style.barView(self.barView, model: self.model) |
| 105 | + Self.Style.stripedLayer(self.stripedLayer, model: self.model) |
| 106 | + Self.Style.handleView(self.handleView, model: self.model) |
| 107 | + Self.Style.handleOverlayView(self.handleOverlayView, model: self.model) |
| 108 | + } |
| 109 | + |
| 110 | + // MARK: - Update |
| 111 | + |
| 112 | + public func update(_ oldModel: SliderVM) { |
| 113 | + guard self.model != oldModel else { return } |
| 114 | + |
| 115 | + self.style() |
| 116 | + |
| 117 | + if self.model.shouldUpdateLayout(oldModel) { |
| 118 | + self.barViewConstraints.height?.constant = self.model.trackHeight |
| 119 | + self.backgroundViewConstraints.height?.constant = self.model.trackHeight |
| 120 | + self.handleViewConstraints.height?.constant = self.model.handleSize.height |
| 121 | + self.handleViewConstraints.width?.constant = self.model.handleSize.width |
| 122 | + |
| 123 | + UIView.performWithoutAnimation { |
| 124 | + self.layoutIfNeeded() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + self.updateSliderAppearance() |
| 129 | + } |
| 130 | + |
| 131 | + private func updateSliderAppearance() { |
| 132 | + if self.model.style == .striped { |
| 133 | + self.stripedLayer.frame = self.backgroundView.bounds |
| 134 | + self.stripedLayer.path = self.model.stripesBezierPath(in: self.stripedLayer.bounds).cgPath |
| 135 | + } |
| 136 | + |
| 137 | + let barWidth = self.model.barWidth(for: self.bounds.width, progress: self.progress) |
| 138 | + self.barViewConstraints.width?.constant = barWidth |
| 139 | + } |
| 140 | + |
| 141 | + // MARK: - Layout |
| 142 | + |
| 143 | + private func layout() { |
| 144 | + self.barViewConstraints = .merged { |
| 145 | + self.barView.leading() |
| 146 | + self.barView.centerVertically() |
| 147 | + self.barView.height(self.model.trackHeight) |
| 148 | + self.barView.width(0) |
| 149 | + } |
| 150 | + |
| 151 | + self.backgroundViewConstraints = .merged { |
| 152 | + self.backgroundView.trailing() |
| 153 | + self.backgroundView.centerVertically() |
| 154 | + self.backgroundView.height(self.model.trackHeight) |
| 155 | + } |
| 156 | + |
| 157 | + self.handleViewConstraints = .merged { |
| 158 | + self.handleView.after(self.barView, padding: self.model.trackSpacing) |
| 159 | + self.handleView.before(self.backgroundView, padding: self.model.trackSpacing) |
| 160 | + self.handleView.size( |
| 161 | + width: self.model.handleSize.width, |
| 162 | + height: self.model.handleSize.height |
| 163 | + ) |
| 164 | + self.handleView.centerVertically() |
| 165 | + } |
| 166 | + |
| 167 | + self.handleOverlayView.center() |
| 168 | + self.handleOverlayView.size( |
| 169 | + width: self.model.handleOverlaySide, |
| 170 | + height: self.model.handleOverlaySide |
| 171 | + ) |
| 172 | + } |
| 173 | + |
| 174 | + open override func layoutSubviews() { |
| 175 | + super.layoutSubviews() |
| 176 | + |
| 177 | + self.backgroundView.layer.cornerRadius = |
| 178 | + self.model.cornerRadius(for: self.backgroundView.bounds.height) |
| 179 | + |
| 180 | + self.barView.layer.cornerRadius = |
| 181 | + self.model.cornerRadius(for: self.barView.bounds.height) |
| 182 | + |
| 183 | + self.handleView.layer.cornerRadius = |
| 184 | + self.model.cornerRadius(for: self.handleView.bounds.width) |
| 185 | + |
| 186 | + self.handleOverlayView.layer.cornerRadius = |
| 187 | + self.model.cornerRadius(for: self.handleOverlayView.bounds.width) |
| 188 | + |
| 189 | + self.updateSliderAppearance() |
| 190 | + self.model.validateMinMaxValues() |
| 191 | + } |
| 192 | + |
| 193 | + // MARK: - UIView Methods |
| 194 | + |
| 195 | + open override func sizeThatFits(_ size: CGSize) -> CGSize { |
| 196 | + let width = self.superview?.bounds.width ?? size.width |
| 197 | + return CGSize( |
| 198 | + width: min(size.width, width), |
| 199 | + height: min(size.height, self.model.handleSize.height) |
| 200 | + ) |
| 201 | + } |
| 202 | + |
| 203 | + open override func touchesBegan( |
| 204 | + _ touches: Set<UITouch>, |
| 205 | + with event: UIEvent? |
| 206 | + ) { |
| 207 | + guard let point = touches.first?.location(in: self), |
| 208 | + self.hitTest(point, with: nil) == self.handleView |
| 209 | + else { return } |
| 210 | + |
| 211 | + self.isDragging = true |
| 212 | + } |
| 213 | + |
| 214 | + open override func touchesMoved( |
| 215 | + _ touches: Set<UITouch>, |
| 216 | + with event: UIEvent? |
| 217 | + ) { |
| 218 | + guard self.isDragging, |
| 219 | + let translation = touches.first?.location(in: self) |
| 220 | + else { return } |
| 221 | + |
| 222 | + let totalWidth = self.bounds.width |
| 223 | + let sliderWidth = max(0, totalWidth - self.model.handleSize.width - 2 * self.model.trackSpacing) |
| 224 | + |
| 225 | + let newOffset = translation.x - self.model.trackSpacing - self.model.handleSize.width / 2 |
| 226 | + let clampedOffset = min(max(newOffset, 0), sliderWidth) |
| 227 | + |
| 228 | + self.currentValue = self.model.steppedValue(for: clampedOffset, trackWidth: sliderWidth) |
| 229 | + } |
| 230 | + |
| 231 | + open override func touchesEnded( |
| 232 | + _ touches: Set<UITouch>, |
| 233 | + with event: UIEvent? |
| 234 | + ) { |
| 235 | + self.isDragging = false |
| 236 | + } |
| 237 | + |
| 238 | + open override func touchesCancelled( |
| 239 | + _ touches: Set<UITouch>, |
| 240 | + with event: UIEvent? |
| 241 | + ) { |
| 242 | + self.isDragging = false |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +// MARK: - Style Helpers |
| 247 | + |
| 248 | +extension UKSlider { |
| 249 | + fileprivate enum Style { |
| 250 | + static func backgroundView(_ view: UIView, model: SliderVM) { |
| 251 | + view.backgroundColor = model.color.background.uiColor |
| 252 | + if model.style == .striped { |
| 253 | + view.backgroundColor = .clear |
| 254 | + } |
| 255 | + view.layer.cornerRadius = model.cornerRadius(for: view.bounds.height) |
| 256 | + view.layer.masksToBounds = true |
| 257 | + } |
| 258 | + |
| 259 | + static func barView(_ view: UIView, model: SliderVM) { |
| 260 | + view.backgroundColor = model.color.main.uiColor |
| 261 | + view.layer.cornerRadius = model.cornerRadius(for: view.bounds.height) |
| 262 | + view.layer.masksToBounds = true |
| 263 | + } |
| 264 | + |
| 265 | + static func stripedLayer(_ layer: CAShapeLayer, model: SliderVM) { |
| 266 | + layer.fillColor = model.color.main.uiColor.cgColor |
| 267 | + switch model.style { |
| 268 | + case .light: |
| 269 | + layer.isHidden = true |
| 270 | + case .striped: |
| 271 | + layer.isHidden = false |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + static func handleView(_ view: UIView, model: SliderVM) { |
| 276 | + view.backgroundColor = model.color.main.uiColor |
| 277 | + view.layer.cornerRadius = model.cornerRadius(for: model.handleSize.width) |
| 278 | + view.layer.masksToBounds = true |
| 279 | + } |
| 280 | + |
| 281 | + static func handleOverlayView(_ view: UIView, model: SliderVM) { |
| 282 | + view.isVisible = model.isHandleOverlayVisible |
| 283 | + view.backgroundColor = model.color.contrast.uiColor |
| 284 | + view.layer.cornerRadius = model.cornerRadius(for: model.handleOverlaySide) |
| 285 | + } |
| 286 | + } |
| 287 | +} |
0 commit comments