3

I am trying to animate text to make it scroll across the screen, (using it to make a stock app), I am unable to get it to go completely off the screen can someone please help...

This is what I have so far

let text = "Some text to animate" private var is = true var body: some View { VStack { Text(text) .fixedSize() .frame(width: 100, alignment: is ? .trailing : .leading) .animation(Animation.linear(duration: 5).repeatForever()) } 
3
  • 3
    This is SwiftUI code. You should use the SwiftUI tag, not Swift. (or at least also add the SwiftUI tag.) Commented Jun 22, 2022 at 13:30
  • Are you trying to make something like a news ticker? Commented Jun 22, 2022 at 15:08
  • Please try out my code example: stackoverflow.com/a/77397865/3013992 Commented Oct 31, 2023 at 17:10

2 Answers 2

4

A possible solution is to use single Text with .move asymmetric transition.

Here is a simplified demo. Tested with Xcode 13.4 / iOS 15.5

demo

Main part:

var body: some View { GeometryReader { gp in VStack { Text(text) .fixedSize() .frame(width: gp.size.width + textWidth, alignment: .trailing) .id(go) .transition(transition) .onAppear{ go.toggle() } .animation(animation, value: go) } }.fixedSize(horizontal: false, vertical: true) } 

Test module on GitHub

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, toggle go back and switch animation to .default, like in stackoverflow.com/a/62080312/12299030.
It seems that just stops the animation whereas I am trying to make it pause so that if resumed it continues from where it left off
1

like this? It shifts the text using .offset if go is true.

 let text = "Some text to animate" @State private var go = false var body: some View { VStack { Text(text) .fixedSize() .frame(width: 100) .offset(x: go ? 300 : 0, y: 0) .animation(Animation.linear(duration: 3).repeatForever(), value: go) .onAppear{self.go.toggle()} } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.