Created
February 12, 2024 20:26
-
-
Save christianselig/eda075422436f4e4934565da001455d4 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 ContentView: View { | |
var body: some View { | |
Slidey() | |
} | |
} | |
struct Slidey: View { | |
let width: CGFloat = 200.0 | |
@State var progress = 0.5 | |
@State var isGestureActive = false | |
var body: some View { | |
Capsule() | |
.foregroundStyle(Color.black) | |
.overlay(alignment: .leading) { | |
Capsule() | |
.foregroundStyle(Color.green) | |
.frame(width: width * progress) | |
} | |
.frame(width: width, height: isGestureActive ? 40.0 : 20.0) | |
.animation(.default, value: isGestureActive) | |
.gesture(DragGesture() | |
.onChanged { value in | |
if !isGestureActive { | |
isGestureActive = true | |
} | |
progress = value.location.x / width | |
} | |
.onEnded { value in | |
isGestureActive = false | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You probably want this in a clipShape anyway to handle the slider getting flattened at the beginning.