Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable default button click animation in SwiftUi

How can i disable the default Button click animation in SwiftUI and Swift 5? I tried to add .animation(.nil) to the button, without any changes.

I know that you can do the following:

Button(action: {}) { Capsule() }
.buttonStyle(NoAnim())

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

Does anybody know a smarter way?

like image 312
LucasMucSO Avatar asked May 04 '20 13:05

LucasMucSO


2 Answers

If I correctly understood your question, then it is better to use just

Capsule()
  .onTapGesture {
    // << action here !!
  }
like image 50
Asperi Avatar answered Sep 25 '22 02:09

Asperi


iOS 13.x, Swift 5.

So you want something that is clickable, but not a button. Just use a label with a onTapGesture on it and then you can add whatever animation you like.

Alternatively you could use the onDrag gesture like this too. This will update the dragLocation as soon as you touch it. So it is like touch event. It also doesn't have any animation liked to it either. That you can add if you so wish.

Text("Hello World")
.accessibility(label: Text("Button"))
.gesture(
    DragGesture(minimumDistance: 5, coordinateSpace: .global)
        .onChanged { value in
          self.dragLocation = value.location
        }
        .onEnded { _ in
          self.dragLocation = .zero
        }
)
like image 40
user3069232 Avatar answered Sep 23 '22 02:09

user3069232