Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created December 16, 2021 21:24
Show Gist options
  • Save cemolcay/8cf7a413e4fcc20bc8c456bc0a5832be to your computer and use it in GitHub Desktop.
Save cemolcay/8cf7a413e4fcc20bc8c456bc0a5832be to your computer and use it in GitHub Desktop.
LiveFader SwiftUI Bridge
//
// LiveFaderSwiftUI.swift
// ControlBud-SwiftUI
//
// Created by Cem Olcay on 11/4/21.
//
import UIKit
import SwiftUI
import LiveFader
struct LiveFaderRep: UIViewRepresentable {
typealias UIViewType = LiveFaderView
@Binding var value: Double
@Binding var isEnabled: Bool
@Binding var isHighlighted: Bool
var minValue: Double = 0.0
var maxValue: Double = 1.0
var baseColor: UIColor = .lightGray
var progressColor: UIColor = .blue
var highlightedBaseColor: UIColor?
var highlightedProgressColor: UIColor?
var disabledBaseColor: UIColor? = .lightGray
var disabledProgressColor: UIColor? = .blue
var controlStyle: LiveFaderView.ControlStyle = .fromBottom
var controlDirection: LiveFaderView.ControlDirection = .vertical
var valueDidChange: ((Double) -> Void)?
var didStartEditing: ((Double) -> Void)?
var didEndEditing: ((Double) -> Void)?
init(
value: Binding<Double> = .constant(0.0),
isEnabled: Binding<Bool> = .constant(true),
isHighlighted: Binding<Bool> = .constant(false)) {
_value = value
_isEnabled = isEnabled
_isHighlighted = isHighlighted
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIView(context: Context) -> LiveFaderView {
let control = LiveFaderView()
control.setupColors()
control.addTarget(context.coordinator, action: #selector(Coordinator.valueDidChange(sender:)), for: .valueChanged)
control.addTarget(context.coordinator, action: #selector(Coordinator.didStartEditing(sender:)), for: .editingDidBegin)
control.addTarget(context.coordinator, action: #selector(Coordinator.didEndEditing(sender:)), for: .editingDidEnd)
return control
}
func updateUIView(_ uiView: LiveFaderView, context: Context) {
uiView.isEnabled = isEnabled
uiView.isHighlighted = isHighlighted
uiView.value = value
uiView.minValue = minValue
uiView.maxValue = maxValue
uiView.faderEnabledBackgroundColor = baseColor
uiView.faderEnabledForegroundColor = progressColor
uiView.faderHighlightedBackgroundColor = highlightedBaseColor
uiView.faderHighlightedForegroundColor = highlightedProgressColor
uiView.faderDisabledBackgroundColor = disabledBaseColor
uiView.faderDisabledForegroundColor = disabledProgressColor
uiView.style = controlStyle
uiView.direction = controlDirection
}
func range(minValue: Double = 0.0, maxValue: Double = 1.0) -> LiveFaderRep {
var control = self
control.minValue = minValue
control.maxValue = maxValue
return control
}
func colors(base: UIColor = .lightGray, progress: UIColor = .blue, disabledBase: UIColor = .lightGray, disabledProgress: UIColor = .blue, highlightedBase: UIColor? = nil, highlightedProgress: UIColor? = nil) -> LiveFaderRep {
var control = self
control.baseColor = base
control.progressColor = progress
control.highlightedBaseColor = highlightedBase
control.highlightedProgressColor = highlightedProgress
control.disabledBaseColor = disabledBase
control.disabledProgressColor = disabledProgress
return control
}
func controlStyle(_ style: LiveFaderView.ControlStyle) -> LiveFaderRep {
var control = self
control.controlStyle = style
return control
}
func controlDirection(_ direction: LiveFaderView.ControlDirection) -> LiveFaderRep {
var control = self
control.controlDirection = direction
return control
}
func onValueChange(_ action: @escaping (Double) -> Void) -> LiveFaderRep {
var control = self
control.valueDidChange = action
return control
}
func onStartEditing(_ action: @escaping (Double) -> Void) -> LiveFaderRep {
var control = self
control.didStartEditing = action
return control
}
func onEndEditing(_ action: @escaping (Double) -> Void) -> LiveFaderRep {
var control = self
control.didEndEditing = action
return control
}
class Coordinator: NSObject {
var control: LiveFaderRep
init(_ control: LiveFaderRep) {
self.control = control
}
@objc func valueDidChange(sender: LiveFaderView) {
control.value = sender.value
control.valueDidChange?(sender.value)
}
@objc func didStartEditing(sender: LiveFaderView) {
control.didStartEditing?(sender.value)
}
@objc func didEndEditing(sender: LiveFaderView) {
control.didEndEditing?(sender.value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment