SwiftUI Button animation

578 Views Asked by At

I use ButtonStyle, for button style. But when I updated button from enable to disable, and change thread, the button change without animation.

//I have ButtonStyle:
struct WizardButtonStyle: ButtonStyle {
    enum Style { case enabled, disabled }
    let style: Style
    
    init (_ style: Style = .enabled) {
        self.style = style
    }
    
    func makeBody(configuration: Configuration) -> some View {
        let color: Color = self.style == .enabled ? .black : .gray
        let effect = self.style == .enabled ? 1 : 0.9
        
        configuration.label
            .frame(width: UIScreen.screenWidth - 36, height: 48, alignment: .center)
            .background(.white)
            .foregroundColor(color)
            .cornerRadius(20)
            .overlay {
                RoundedRectangle(cornerRadius: 20)
                    .stroke(color, lineWidth: 3)
            }
            .scaleEffect(effect)
    }
 
}
//Button: 

    func wizardButtonView() -> some View {
        let title = "Title"

        let button = Button {
            if self.configure.buttonStyle == .disabled { return }
            self.configure.buttonStyle = .disabled
            self.configure.buttonAction()
        } label: {
            Text(title)
                .font(.custom(Fonts.unswer.rawValue, fixedSize: 28))
                .multilineTextAlignment(.center)
                .lineLimit(1)
        }
            .buttonStyle(WizardButtonStyle(self.configure.buttonStyle))
        
        return button
    }

//
some call like 
.task {
    //default state = enable
    state = .disable // animated change button state
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        //change after server resonce
        state = .enable // button change without animatio 
    }
}

}

I use ButtonStyle, for button style. But when I updated button from enable to disable, and change thread, the button change without animation.

0

There are 0 best solutions below