Skip to content

Instantly share code, notes, and snippets.

@GoodRequestGR
Last active July 19, 2022 09:00
Show Gist options
  • Save GoodRequestGR/6e1cf580acaac35582ca4472013ca5f4 to your computer and use it in GitHub Desktop.
Save GoodRequestGR/6e1cf580acaac35582ca4472013ca5f4 to your computer and use it in GitHub Desktop.
How to Make a Slide to Unlock Button in SwiftUI
struct DraggingComponent: View {
let maxWidth: CGFloat
private let minWidth = CGFloat(50)
@State private var width = CGFloat(50)
var body: some View {
RoundedRectangle(cornerRadius: 16)
.fill(Color.blueDark)
.frame(width: width)
.gesture(
DragGesture()
.onChanged { value in
if value.translation.width > 0 {
width = min(max(value.translation.width + minWidth, minWidth), maxWidth)
}
}
)
.animation(.spring(response: 0.5, dampingFraction: 1, blendDuration: 0), value: width)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment