Created
March 15, 2025 16:35
-
-
Save Matt54/19033b46bffa4220cbd49912bd0f43c4 to your computer and use it in GitHub Desktop.
SwiftUI Slider with track fill from center
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
#Preview { | |
@Previewable @State var value: Double = 0.0 | |
VStack { | |
Text("Value: \(value)") | |
CenterSliderWithIcons(value: $value) | |
} | |
.foregroundStyle(.black) | |
.padding() | |
.frame(maxHeight: .infinity) | |
.background(Color.init(white: 0.95)) | |
} | |
struct CenterSliderWithIcons: View { | |
@Binding var value: Double | |
var bounds: ClosedRange<Double> = -1...1 | |
var body: some View { | |
HStack { | |
Image(systemName: "arrow.down") | |
.symbolEffect(.bounce, options: .repeat(1), | |
isActive: value == bounds.lowerBound) | |
CenterSlider(value: $value, bounds: bounds) | |
Image(systemName: "arrow.up") | |
.symbolEffect(.bounce, options: .repeat(1), | |
isActive: value == bounds.upperBound) | |
} | |
} | |
} | |
struct CenterSlider: View { | |
@Binding var value: Double | |
var bounds: ClosedRange<Double> = -1...1 | |
var travelLength: Double { | |
bounds.upperBound - bounds.lowerBound | |
} | |
// 0 to 1 in range | |
var normalizedValue: Double { | |
return (value - bounds.lowerBound) / travelLength | |
} | |
var body: some View { | |
Slider(value: $value, in: bounds) | |
.overlay( | |
GeometryReader { proxy in | |
let trackWidth = proxy.size.width - proxy.size.height | |
ZStack(alignment: .leading) { | |
// track | |
RoundedRectangle(cornerRadius: proxy.size.height*0.5) | |
.fill(Color.init(white: 0.75)) | |
.overlay( | |
RoundedRectangle(cornerRadius: proxy.size.height*0.5) | |
.stroke(Color.init(white: 0.5), lineWidth: 1) | |
) | |
// inside fill | |
RoundedRectangle(cornerRadius: proxy.size.height*0.5) | |
.fill(.blue) | |
.frame(width: trackWidth * CGFloat(abs(0.5-normalizedValue)) + proxy.size.height) | |
.offset(x: (0.5*trackWidth - max(0,0.5-normalizedValue) * trackWidth)) | |
// thumb | |
Circle() | |
.fill(.white) | |
.offset(x: trackWidth * normalizedValue) | |
.padding(1) | |
} | |
} | |
.allowsHitTesting(false) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment