0

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.

Medoed
  • 1
  • 3
  • 1
    Firstly you need to note, that all updates on UI are doing in main thread. Also, there some lack of information, what is state? How it is written, is it allow to react on changes? And finally I see you missed .animation on your button property, which is triggered when some value changed – Arsienij Aug 04 '23 at 09:05
  • Hello! Your code doesn't seem to work "out of the box", so I modified it a little. It seems to be working for me. Could you have a look: https://gist.github.com/Baglan/75cf2d4e56e156ad47c286861f2492ad – Baglan Aug 04 '23 at 09:17
  • @Baglan thx withAnimation help me – Medoed Aug 04 '23 at 17:46

0 Answers0