Hello. I am here to report a problem.
I was investigating the strange behaviour of my text animation, and I've found out that unrelated TimelineView
is causing the problem.
text is animating on change and there are colors on background (in ZStack
) that change using TimelineView
.
As a result, when the text changes, it twitches, until it fills the whole screen width. If I pause TimelineView
animation, everything works smoothly
The problem occurs only on a real device. I've tested on iOS 17 and 18
Example code
struct ContentView: View {
@State var open = false
let colors: [Color] = [.cyan, .red, .green, .gray, .blue, .orange, .pink, .purple]
@State var i: Int = 0
@State
var text: String = chunks[0]
@State var pause = false
var body: some View {
ZStack {
TimelineView(.animation(minimumInterval: 1.2, paused: pause)) { context in
let color = colors[Int(context.date.timeIntervalSinceReferenceDate) % colors.count]
ZStack {
color
.animation(.easeInOut(duration: 1.2))
Rectangle().fill(Material.regular).edgesIgnoringSafeArea(.all)
}
}
VStack {
Spacer()
Text(text)
.font(.system(size: 32))
.multilineTextAlignment(.center)
.padding()
.animation(.linear(duration: 0.1), value: text)
Spacer()
Button("Add text") {
if i < (chunks.count - 1) {
i += 1
} else {
i = 0
text = ""
}
text.append(" " + chunks[i])
}
Button("Toggle background animation") {
pause.toggle()
}
}
}
}
}
let lyrics = "I'm born to run down rocky cliffs Give me grace, bury my sins Shattered glass and black holes Can't hold me back from where I need to go"
let chunks = lyrics.components(separatedBy: " ")