-
-
Save sturdysturge/b984c312b33d4ab1fb6e006363acf22e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| 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