Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify custom ButtonStyle animations

You can modify the highlight/press state of Buttons with SwiftUI by implementing your own ButtonStyle which modifies the label based on the isPressed state.

In iOS 13, this was animated both on touch down and touch up. In iOS 14 beta, it does not animate on press down, only up. I want to enable animation on touch down. How can this be done?

struct CustomButtonStyle: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.5 : 1)
    }
}

struct CustomButton: View {
    var body: some View {
        Button(action: {}) {
            RoundedRectangle(cornerRadius: 10)
                .frame(width: 100, height: 100)
                .foregroundColor(.pink)
        }
        .buttonStyle(CustomButtonStyle())
    }
}

Recording of difference between iOS 14 and 13

like image 981
Jordan H Avatar asked Dec 29 '25 04:12

Jordan H


1 Answers

Simply define an explicit animation in your button style make body. for example, you can add .animation(.easeOut) below .scaleEffect(configuration.isPressed ? 0.5 : 1)

like image 74
Mahdi BM Avatar answered Dec 30 '25 21:12

Mahdi BM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!