Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active February 27, 2023 04:48
Show Gist options
  • Select an option

  • Save sturdysturge/b984c312b33d4ab1fb6e006363acf22e to your computer and use it in GitHub Desktop.

Select an option

Save sturdysturge/b984c312b33d4ab1fb6e006363acf22e to your computer and use it in GitHub Desktop.
import SwiftUI
struct SliderThumbView: View {
static let thumbHeight = CGFloat(28)
@Environment(\.colorScheme) var colorScheme
@Binding var value: Double
let bounds: ClosedRange<Double>
var shadowColour: Color {
colorScheme == .light ? Color(white: 0.85) : .clear
}
var body: some View {
GeometryReader { proxy in
let maxOffset = proxy.frame(in: .global).width - Self.thumbHeight
Circle()
.foregroundColor(.white)
.frame(maxWidth: .infinity, alignment: .leading)
.offset(x: (value / bounds.upperBound) * maxOffset)
.shadow(color: shadowColour, radius: 3, x: 1, y: 3)
.gesture(
DragGesture()
.onChanged { gesture in
if gesture.location.x < 1 {
value = bounds.lowerBound
} else if gesture.location.x > maxOffset {
value = bounds.upperBound
} else {
let amount = gesture.location.x / maxOffset
value = amount * bounds.upperBound
}
}
)
}
.frame(height: Self.thumbHeight)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment