animationswiftuitransition

SwiftUI springy scale "pop" transition


Given some shape, I want to make it pop onto the screen.

if showShape {
  Rectangle()
    .frame(width: 100, height: 50)
    .transition(.scale)
}

How could a custom Transition replace the scale animation in this example with one that uses springlike physics to scale the shape when it appears when showShape gets toggled by the user?


Solution

  • From my understanding of what you're trying to make, here is the code that makes the scale effect springy:

    if showShape {
      Rectangle()
        .frame(width: 100, height: 50)
        .transition(.scale.animation(.spring))
    }
    

    And you can make it extra springy with .springy(bounce: <#Double#>)