如何修改自定义ButtonStyle动画

时间:2020-08-23 18:32:35

标签: ios button swiftui

您可以通过实现自己的Button并根据ButtonStyle状态修改label来使用SwiftUI修改isPressed的高亮/按下状态。

在iOS 13中,此动画在触地和触地时均具有动画效果。在iOS 14 Beta中,它不会在按下时动画,而仅在向上动画。我想启用触地动画。该怎么办?

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

1 个答案:

答案 0 :(得分:1)

只需以按钮样式npm install --save-dev node-sass sass-loader定义一个明确的动画。 例如,您可以在make body下添加.animation(.easeOut)