This repository contains a custom SwiftUI modifier for adding a 'wiggle' animation to any SwiftUI View
. The idea was inspired by markmals on GitHub. This implementation extends the original idea by allowing the animation to be controlled by a boolean state variable and providing custom rotation and bounce amounts.
Simulator.Screen.Recording.-.iPhone.14.Pro.-.2023-05-12.at.17.20.15.mp4
Just copy the WiggleModifier.swift
file into your project.
In your SwiftUI View
, you can use the wiggling
modifier like this:
struct ContentView: View {
@State private var isWiggling = false
var body: some View {
VStack {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(.blue)
.wiggling(isWiggling: $isWiggling, rotationAmount: 3, bounceAmount: 2)
Button(action: {
withAnimation {
self.isWiggling.toggle()
}
}) {
Text(self.isWiggling ? "Disable Animation" : "Enable Animation")
}
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(10)
}
}
}
The wiggling
modifier takes three arguments:
isWiggling
: ABinding<Bool>
that controls whether the wiggle animation is active.bounceAmount
: ADouble
that controls the bounce amount of the wiggle animation.rotationAmount
: ADouble
that controls the rotation amount of the wiggle animation.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.