Here's an example of how to animate scaling a rectangle from 0.5 to 1.5 forever in SwiftUI using withAnimation
:
main.swift402 chars16 lines
In this example, we use the scaleEffect
modifier to scale the rectangle based on the scale
state variable. We then use the animation
modifier to specify the animation we want to run. We're using the easeInOut
timing curve with a 1 second duration, and calling repeatForever
to create an animation that loops indefinitely.
Finally, we use the onAppear
modifier to trigger the animation by changing the scale
state variable from 0.5 to 1.5 with the withAnimation
closure.
You can also achieve this using the animation(_:value:)
function. Here's an example:
main.swift527 chars18 lines
In this example, we use the animation(_:value:)
function to specify the animation and the scale
state variable as the value that triggers the animation. We're also using a Timer
to switch between the scale
values of 0.5 and 1.5 every 2 seconds.
gistlibby LogSnag