Skip to content

Instantly share code, notes, and snippets.

@JaydenIrwin
Created May 15, 2020 17:11
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 JaydenIrwin/f81fe54e8b9c4b9cba5499ebec4ef407 to your computer and use it in GitHub Desktop.
Save JaydenIrwin/f81fe54e8b9c4b9cba5499ebec4ef407 to your computer and use it in GitHub Desktop.
The characters in the string "do the wave"!
import SwiftUI
struct FloatEffect: GeometryEffect {
var animatableData: CGFloat
func effectValue(size: CGSize) -> ProjectionTransform {
ProjectionTransform(CGAffineTransform(translationX: 0, y: sin(animatableData) * 12))
}
}
struct AnimatedWaveText: View {
@State var chars: [String]
@State var sinOffset: CGFloat = 0.0
var body: some View {
HStack {
ForEach(chars.indices, id: \.self) { index in
Text(self.chars[index])
.foregroundColor(Color(hue: Double(index) / Double(self.chars.count), saturation: 1.0, brightness: 1.0))
.modifier(FloatEffect(animatableData: CGFloat(index) + self.sinOffset))
}
}
.onAppear() {
withAnimation(Animation.linear(duration: 4.0).repeatForever(autoreverses: false), {
self.sinOffset = 2 * .pi
})
}
}
init(_ string: String) {
_chars = State(initialValue: string.map({ String($0) }))
}
}
struct AnimatedWaveText_Previews: PreviewProvider {
static var previews: some View {
AnimatedWaveText("With Friends")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment