Skip to content

Instantly share code, notes, and snippets.

@amosgyamfi
Created March 18, 2022 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amosgyamfi/37bf602c0fd145e3f5a8e6384dd5f796 to your computer and use it in GitHub Desktop.
Save amosgyamfi/37bf602c0fd145e3f5a8e6384dd5f796 to your computer and use it in GitHub Desktop.
//
// DraggableMenu.swift
// HundredDaysOfSwiftUI
//
// Created by Amos
//
import SwiftUI
struct DraggableMenu: View {
let letters = Array("▬▬▬")
@State private var enabled = false
@State private var dragAmount = CGSize.zero
var body: some View {
VStack(spacing: 0) {
ForEach(0..<letters.count) { num in
Text(String(letters[num]))
.padding(-10)
.font(.largeTitle)
.offset(dragAmount)
.animation(.interpolatingSpring(stiffness: 170, damping: 5).delay(Double(num) / 20), value: dragAmount)
}
}
.padding()
.scaleEffect(x: 3)
.gesture(
DragGesture()
.onChanged { dragAmount = $0.translation }
// _ ignore the value coming in this time
.onEnded { _ in
withAnimation{
dragAmount = .zero
enabled.toggle()
}
}
)
}
}
struct DraggableMenu_Previews: PreviewProvider {
static var previews: some View {
DraggableMenu()
.preferredColorScheme(.dark)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment