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.

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 Commented Aug 4, 2023 at 9: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: gist.github.com/Baglan/75cf2d4e56e156ad47c286861f2492ad Commented Aug 4, 2023 at 9:17
  • @Baglan thx withAnimation help me Commented Aug 4, 2023 at 17:46

0

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.