I want to make Text View with dots that increase and decrease over time.
but it looks twitchy and smudged. And also the text itself is shifted every time. How to get rid of it?
Here is my code:
struct TextViewTest: View {
@State var dotsSwitcher = 0
var body: some View {
Text("Loading\(dots)")
.animation(.easeOut(duration: 0.1), value: dotsSwitcher)
.onReceive(Timer.publish(every: 1, on: .main, in: .common)
.autoconnect()) { _ in dotsAnimation() }
.onAppear(perform: dotsAnimation)
}
var dots: String {
switch dotsSwitcher {
case 1: return "."
case 2: return ".."
case 3: return "..."
default: return ""
}
}
func dotsAnimation() {
withAnimation {
dotsSwitcher = 0
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
withAnimation {
dotsSwitcher = 1
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
withAnimation {
dotsSwitcher = 2
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
withAnimation {
dotsSwitcher = 3
}
}
}
}