1

I have a loading screen where I want to show a text changing its value automatically with an animation.

I have my logo rotating indefinitely without any button action

//logo Image("reny") .rotationEffect(.degrees(rotateDegree)) .onAppear(perform: { withAnimation(Animation.linear(duration: 4).repeatForever(autoreverses: false)) { self.rotateDegree = 360 } }) 

I assumed it was possible to do the same for a text using a string array but it doesn't work

@State var texts = ["Find the Apartment you like", "send an application", "we'll approve you in secs baby!"] @State var textIndex : Int = 0 
//introduction text Text(texts[textIndex]).bold() .font(.title) .onAppear(perform: { withAnimation(Animation.linear(duration: 2).repeatForever(autoreverses: false)) { textIndex += 1 } }) 

does anybody know how to change the value of a text with an animation automatically?

my intention is to show how to use the app during this loading time.

3
  • Which do you want? Transitions and Animations are two different things. Transitions deal with exchanging one view for another, where animations deal with a change within one view. Commented Jun 22, 2022 at 1:39
  • animation I guess, I just want the text value to change and loop based on the arrays value Commented Jun 22, 2022 at 1:55
  • The code is working fine here. What do you expect to happen vs. what is happening. Also, to read;;y helps to produce a Minimal Reproducible Example (MRE). I had to create a view to plug this into, and you may be doing something different, but are not showing it. This goes along with the tour and see: How do I ask a good question?. Commented Jun 22, 2022 at 2:02

1 Answer 1

2

Here is a possible approach - just to replace Text depending on index and continuously change index after appear.

Tested with Xcode 13.4 / iOS 15.5

demo

Main part:

private func next() { var next = textIndex + 1 if next == texts.count { next = 0 } withAnimation(Animation.linear(duration: 2)) { textIndex = next } DispatchQueue.main.asyncAfter(deadline: .now() + 2) { self.next() } } 

and text itself

Text(texts[textIndex]).bold() .font(.title).id(textIndex) .onAppear(perform: { next() }) 

Test module on GitHub

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

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.