Created
May 15, 2020 17:11
-
-
Save JaydenIrwin/f81fe54e8b9c4b9cba5499ebec4ef407 to your computer and use it in GitHub Desktop.
The characters in the string "do the wave"!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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