Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created December 16, 2021 21:24
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cemolcay/caed8e701de775de63ab4ae34b70b256 to your computer and use it in GitHub Desktop.
LiveKnob SwiftUI Bridge
//
// LiveKnobSwiftUI.swift
// ControlBud-SwiftUI
//
// Created by Cem Olcay on 11/3/21.
//
import UIKit
import SwiftUI
import LiveKnob
struct LiveKnobRep: UIViewRepresentable {
typealias UIViewType = LiveKnob
var value: Binding<Double>
var isEnabled: Binding<Bool>
var minValue: Double = 0.0
var maxValue: Double = 1.0
var baseColor: UIColor = .black
var pointerColor: UIColor = .black
var progressColor: UIColor = .orange
var baseLineWidth: CGFloat = 2.0
var progressLineWidth: CGFloat = 2.0
var pointerLineWidth: CGFloat = 2.0
var controlType: LiveKnobControlType = .rotary
var valueDidChange: ((Double) -> Void)?
var didStartEditing: ((Double) -> Void)?
var didEndEditing: ((Double) -> Void)?
init(
value: Binding<Double> = .constant(0.0),
isEnabled: Binding<Bool> = .constant(true)) {
self.value = value
self.isEnabled = isEnabled
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIView(context: Context) -> LiveKnob {
let knob = LiveKnob()
knob.addTarget(context.coordinator, action: #selector(Coordinator.valueDidChange(sender:)), for: .valueChanged)
knob.addTarget(context.coordinator, action: #selector(Coordinator.didStartEditing(sender:)), for: .editingDidBegin)
knob.addTarget(context.coordinator, action: #selector(Coordinator.didEndEditing(sender:)), for: .editingDidEnd)
return knob
}
func updateUIView(_ uiView: LiveKnob, context: Context) {
uiView.isEnabled = isEnabled.wrappedValue
uiView.value = Float(value.wrappedValue)
uiView.minimumValue = Float(minValue)
uiView.maximumValue = Float(maxValue)
uiView.baseColor = baseColor
uiView.pointerColor = pointerColor
uiView.progressColor = progressColor
uiView.baseLineWidth = baseLineWidth
uiView.pointerLineWidth = pointerLineWidth
uiView.progressLineWidth = progressLineWidth
uiView.controlType = controlType
}
func range(minValue: Double = 0.0, maxValue: Double = 1.0) -> LiveKnobRep {
var knob = self
knob.minValue = minValue
knob.maxValue = maxValue
return knob
}
func colors(base: UIColor = .black, pointer: UIColor = .black, progress: UIColor = .orange) -> LiveKnobRep {
var knob = self
knob.baseColor = base
knob.pointerColor = pointer
knob.progressColor = progress
return knob
}
func lineWidths(base: CGFloat = 2, pointer: CGFloat = 2, progress: CGFloat = 2) -> LiveKnobRep {
var knob = self
knob.baseLineWidth = base
knob.pointerLineWidth = pointer
knob.progressLineWidth = progress
return knob
}
func type(_ controlType: LiveKnobControlType) -> LiveKnobRep {
var knob = self
knob.controlType = controlType
return knob
}
func onValueChange(_ action: @escaping (Double) -> Void) -> LiveKnobRep {
var knob = self
knob.valueDidChange = action
return knob
}
func onStartEditing(_ action: @escaping (Double) -> Void) -> LiveKnobRep {
var knob = self
knob.didStartEditing = action
return knob
}
func onEndEditing(_ action: @escaping (Double) -> Void) -> LiveKnobRep {
var knob = self
knob.didEndEditing = action
return knob
}
class Coordinator: NSObject {
var control: LiveKnobRep
init(_ control: LiveKnobRep) {
self.control = control
}
@objc func valueDidChange(sender: LiveKnob) {
control.value.wrappedValue = Double(sender.value)
control.valueDidChange?(Double(sender.value))
}
@objc func didStartEditing(sender: LiveKnob) {
control.didStartEditing?(Double(sender.value))
}
@objc func didEndEditing(sender: LiveKnob) {
control.didEndEditing?(Double(sender.value))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment