代码之家  ›  专栏  ›  技术社区  ›  Jordan H

如何修改自定义按钮样式动画

  •  0
  • Jordan H  · 技术社区  · 4 年前

    您可以修改的突出显示/按下状态 Button 通过实现您自己的功能,您可以使用SwiftUI ButtonStyle 它修改了 label 基于 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

    0 回复  |  直到 4 年前
        1
  •  2
  •   Mahdi BM    4 年前

    只需定义按钮样式中的显式动画 make body . 例如,您可以添加 .animation(.easeOut) 在下面 .scaleEffect(configuration.isPressed ? 0.5 : 1)