6

I have a problem with an animation that involves a Text. Basically I need to change the text and animate its position. Take a look at this simple example here below:

import SwiftUI

struct ContentView: View {
    @State private var isOn = false
    @State private var percentage = 100

    var body: some View {
        ZStack {
            Text("\(percentage)")
                .font(.title)
                .background(Color.red)
                .position(isOn ? .init(x: 150, y: 300) : .init(x: 150, y: 100))
                .animation(.easeOut(duration: 1), value: isOn)

            VStack {
                Spacer()
                Button(action: {
                    self.isOn.toggle()

                    //just to make the issue happen
                    if self.percentage == 100 {
                        self.percentage = 0
                    } else {
                        self.percentage = 100
                    }
                }, label: {
                    Text("SWITCH")
                })
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

The result is:

enter image description here

There are some issues here. Probably the most annoying is the glitch with the ... I just want to animate the position of the Text, I don't want to animate the text itself and I don't want to animate the width of the text. Any ideas? Thank you.

2 Answers 2

4

The possible alternate is to add scaling factor, it supersedes truncation mode and gives different effect, which under some circumstances might be preferable.

The only needed changes is as below (factor modifiable of course)

Text("\(percentage)")
    .minimumScaleFactor(0.5)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It’s not exactly the result I wanted to get, but sometimes it may be useful.
3

Try this action on the button:

 Button(action: 
 {
   //just to make the issue happen
   if self.percentage == 100 
   {
     self.percentage = 0
   } 
   else 
   {
     self.percentage = 100
   }
   withAnimation(.easeInOut(duration: 1.0)) {
     self.isOn.toggle()
   }

 }, label: {
   Text("SWITCH")
 })

And remove that line from your Label

 .animation(.easeOut(duration: 1), value: isOn)

I didn't test it yet.

4 Comments

Thanks, using explicit animations solves the issue. Do you think it’s a SwiftUI bug concerning implicit animations? I’m asking because your solution and mine should be equivalent.
@superpuccio Not sure.. acutally the wrong behaviour of you is correct, as you set the Animation for the whole view and when the text changes, the size is changing too and gets animated.
Yes, but I specified isOn for the value. As far as I know if you specify a value only the modifiers that depend on that value will be animated. In this case the position.
Does not work in my case. I have an image and text in a HStack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.