11

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?

lucaszischka
  • 132
  • 1
  • 1
  • 7

2 Answers2

16

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

Capsule()
  .onTapGesture {
    // << action here !!
  }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
3

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
        }
)
user3069232
  • 8,587
  • 7
  • 46
  • 87
  • do recognize user with Voice Over or other accessibility options this Label as a button? – lucaszischka May 05 '20 at 16:23
  • Lucas. There are many tags you can add to your code to make it more accessible, they are options you need to add specifically, with .accessability modifier. I tested voiceover on the above and edited the code to announce a button when you pass over the text. But on a related/different note the best way to use this site is the post individual questions. There is no limit on the number of posts you make. For the best answers ask a specific question about accessibility in specific posts. – user3069232 May 06 '20 at 05:37