Swift 摆脱按钮的标准onTouch动画

Swift 摆脱按钮的标准onTouch动画,swift,swiftui,Swift,Swiftui,当我在SwiftUI中实现一个按钮时,当你点击它时,这个按钮总是有一个标准的动画。轻微的不透明动画。有没有办法摆脱那种动画? 下面是一个简单的例子 这里是一个小视频-> 通过创建自己的按钮样式,您可以得到想要的。这样,您必须指定要在点击时使用的动画。如果未指定任何动画,则该按钮将不会有任何动画。例如: struct NoAnimationButton: ButtonStyle { func makeBody(configuration: Self.Configuration) ->

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

这里是一个小视频->


通过创建自己的按钮样式,您可以得到想要的。这样,您必须指定要在点击时使用的动画。如果未指定任何动画,则该按钮将不会有任何动画。例如:

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

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)
        .buttonStyle(NoAnimationButton())
    }
}
请考虑,您始终可以避免使用按钮视图,并在整个可点击视图上或需要的任何位置执行ONTAPPISTIVE:

struct ContentView: View {
    var body: some View {
        VStack{
            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)
        .onTapGesture {
            //manage click
        }
    }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
编辑:仅制作带有动画的按钮样式示例:

struct ScaleEffectButton: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.9 : 1)
    }
}
public struct ButtonWithoutAnimation: ButtonStyle {
    public func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
    }
}

通过创建自己的按钮样式,您可以得到想要的。这样,您必须指定要在点击时使用的动画。如果未指定任何动画,则该按钮将不会有任何动画。例如:

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

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)
        .buttonStyle(NoAnimationButton())
    }
}
请考虑,您始终可以避免使用按钮视图,并在整个可点击视图上或需要的任何位置执行ONTAPPISTIVE:

struct ContentView: View {
    var body: some View {
        VStack{
            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)
        .onTapGesture {
            //manage click
        }
    }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
编辑:仅制作带有动画的按钮样式示例:

struct ScaleEffectButton: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.9 : 1)
    }
}
public struct ButtonWithoutAnimation: ButtonStyle {
    public func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
    }
}
您可以定义自己的按钮样式,该样式将覆盖默认动画:

struct ScaleEffectButton: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.9 : 1)
    }
}
public struct ButtonWithoutAnimation: ButtonStyle {
    public func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
    }
}
现在只需在按钮上设置样式:

您可以定义自己的按钮样式,该样式将覆盖默认动画:

struct ScaleEffectButton: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.9 : 1)
    }
}
public struct ButtonWithoutAnimation: ButtonStyle {
    public func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
    }
}
现在只需在按钮上设置样式: