Home > database >  SwiftUI text offset, value change animation to slow
SwiftUI text offset, value change animation to slow

Time:12-15

I want to make toast message but animation is too bad could u fix my animation?

appearing text rendering very slow....
but move to bottom animation so good!

under is simple example
give me back my time, cool developers

import SwiftUI
struct ContentView: View {
  @State var animate: Bool = false
  @State var title: String = "AAAA"
  var body: some View {
    ZStack {
      VStack {
        Spacer()
        OffsetAnimationExample(title: title)
      }
      .offset(y: animate ? .zero: -440)
      .animation(.spring(), value: animate)
      
      Button(action: {
        
        withAnimation(.spring(response: 0.5, dampingFraction: 0.5, blendDuration: 1).repeatForever()) {
          title = ["AAAA","BBBB", "CCCC", "DDDD", "EEE", "FFFFF"].randomElement()!
          animate.toggle()
        }
      }) {
        Text("Tap")
      }
    }
    .background(.red)
  }
}

struct OffsetAnimationExample: View {
  var title: String
  
  var body: some View {
    ZStack {
      RoundedRectangle(cornerRadius: 20)
        .fill(Color.orange)
      Text(title)
        .font(.largeTitle)
        .foregroundColor(.white)
    }
    .frame(width: 200, height: 100)
  }
}

CodePudding user response:

Form your question it look you want change the text animation, Have a look at this code.

import SwiftUI


struct ContentView: View {
    var body: some View {
        OffsetAnimationExample()
    }
}

struct OffsetAnimationExample: View {
    
    @State private var animate = false
    @State private var title = "BBBB"
    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 20)
                .fill(Color.orange)
            Text(title)
                .font(.largeTitle)
                .foregroundColor(.white)
        }
        .frame(width: 200, height: 100)
        .offset(y: animate ? 200 : 0)
        .animation(.spring(), value: animate)
        .onTapGesture {
            title = animate ? "BBBB": "AAAA"
            withAnimation(.spring(response: 0.5, dampingFraction: 0.5, blendDuration: 1).repeatForever()) {
                animate.toggle()
            }
            
        }
    }
}

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