Skip to content

Instantly share code, notes, and snippets.

@amosgyamfi
Created April 19, 2022 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amosgyamfi/36858c4d8466f6d384b091bb90538fa8 to your computer and use it in GitHub Desktop.
Save amosgyamfi/36858c4d8466f6d384b091bb90538fa8 to your computer and use it in GitHub Desktop.
//
// ChainedSpring.swift
// HundredDaysOfSwiftUI
//
//
import SwiftUI
struct ChainedSpring: View {
let letters = Array("SpringAnimation")
@State private var enabled = false
@State private var dragAmount = CGSize.zero
var body: some View {
HStack(spacing: 0) {
ForEach(0..<letters.count) { num in
Text(String(letters[num]))
.padding(5)
.font(.title)
.background(enabled ? .blue : .red)
.hueRotation(.degrees(enabled ? 0 : 150))
.rotationEffect(.degrees(enabled ? 0 : 360), anchor: .bottomLeading)
.border(.white)
.cornerRadius(2)
.offset(dragAmount)
.animation(.interpolatingSpring(stiffness: 170, damping: 5).delay(Double(num) / 20), value: dragAmount)
}
}
.padding()
.gesture(
DragGesture()
.onChanged { dragAmount = $0.translation }
// _ ignore the value coming in this time
.onEnded { _ in
withAnimation{
dragAmount = .zero
enabled.toggle()
}
}
)
}
}
struct ChainedSpring_Previews: PreviewProvider {
static var previews: some View {
ChainedSpring()
.preferredColorScheme(.dark)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment