首页 > 解决方案 > 摆脱按钮的标准 onTouch 动画

问题描述

当我在 SwiftUI 中实现一个 Button 时,当你点击它时,这个 Button 总是有一个标准的动画。轻微的不透明动画。有没有办法摆脱那个动画?这是一个简单的例子。

这是一个小视频-> https://imgur.com/ClWd7YH

import SwiftUI

struct ContentView: View {
var body: some View {

VStack{

  Button(action: {
    // do somethinh
  }) {
    VStack{

      ZStack{
        Circle()
          .fill(Color.white)
          .frame(width: 45, height: 45, alignment: .center)

        Image(systemName: "xmark.circle")
          .foregroundColor(Color.black)
          .font(Font.system(size: 18, weight: .thin))
          .padding(6)
      }

      Text("Press")
        .foregroundColor( Color.white)

    }.frame(width: 300, height: 300, alignment: .center)
  }
}.background(Color.green)
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

标签: swiftswiftui

解决方案


您可以定义自己的ButtonStyle将覆盖默认动画:

public struct ButtonWithoutAnimation: ButtonStyle {
    public func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
    }
}

现在只需在 上设置样式Button

Button(action: { .. } {
..
}.buttonStyle(ButtonWithoutAnimation())

推荐阅读