Creating Superior Animation with KeyframeAnimator in SwiftUI

Spread the love


Along with the PhaseAnimator, SwiftUI launched the KeyframeAnimator in iOS 17, permitting builders to create superior animations utilizing keyframes. On this tutorial, we are going to delve into the KeyframeAnimator and learn to create a extra intricate animation.

The PhaseAnimator view (or modifier), which we mentioned within the earlier tutorial, supplies builders with the power to create multi-step animations over a sequence of phases. By specifying the specified animations for every part, the PhaseAnimator routinely animates the content material each time the part adjustments. It simplifies the method of constructing complicated animations by dealing with the transitions between phases for you.

Working with KeyframeAnimator

For phase-based animation, it really works properly for animations that may be represented as discrete states. When a state transition occurs, all properties are animated concurrently. As soon as the animation for a selected state is accomplished, SwiftUI easily transitions to the subsequent state. This course of continues throughout all animation phases.

Keyframe-based animation is designed to accommodate a particular sort of animation the place every property is animated independently. By using keyframes, we are able to animate particular person properties individually, which in flip gives us better flexibility and management over our animations.

swiftui-keyframeanimator-demo

Let’s attempt to animate an emoji icon (as illustrated above) and you’ll perceive how we are able to use keyframe animator.

Defining the Animation Values

As talked about earlier, keyframe-based animation permits us to animate particular person properties independently. To make the most of the keyframe animator, we start by defining a struct that encompasses all of the properties we want to animate. Right here’s an instance:

The preliminary values outline the preliminary state of the emoji icon. Later, we are going to change every of the properties to scale, stretch, and transfer the emoji icon.

Making use of the Keyframe Animator

Within the physique closure, let’s replace the code like this to use the keyframe animator:

As typical, we use a Textual content view to show the emoji icon. To create a keyframe-based animation, we connect the keyframeAnimator modifier to the textual content view.

The initialValue parameter is supplied with the preliminary values that the keyframes will animate from. Inside the view builder closure, we have now entry to 2 parameters. The primary parameter is a proxy worth that represents the modified view. The second parameter holds the interpolated worth generated by the keyframes.

We apply the specified animation results to the content material view by adjusting its scale, offset, and opacity. Lastly, it’s the keyframes parameter. That is the place we outline the worth adjustments that happen over time. These outlined keyframes will likely be accountable for making use of the corresponding animations to the required worth.

Keyframes are organized into tracks, with every monitor governing a definite property of the animated sort. Within the supplied code snippet, we particularly designated the keyframe monitor for the dimensions property utilizing the CubicKeyframe sort. We regulate the scale of the emoji, decreasing it to 80% of its authentic dimension.

swiftui-keyframeanimator-scale

When you’ve made the code adjustments, it’s best to have the ability to see the animation immediately within the preview canvas. The keyframe animator animates the scale change and repeats it repeatedly.

Keyframe Varieties

Whereas we use the CubicKeyframe sort, there are literally 4 various kinds of keyframes accessible:

  • LinearKeyframe – it interpolates linearly in vector area from the earlier keyframe.
  • SpringKeyframe – makes use of a spring operate to interpolate to the goal worth from the earlier keyframe.
  • CubicKeyframe – makes use of a cubic Bézier curve to interpolate between keyframes.
  • MoveKeyframe – instantly jumps to a price with out interpolation.

Attempt to discover and check completely different keyframe varieties and durations to see their behaviors in motion. By experimenting with numerous keyframe varieties and adjusting the length, you’ll be able to acquire a deeper understanding of how they impression and form your animations.

At present, we solely apply a single change for the scale property. You might be free to outline different worth adjustments over time. Right here is an instance:

The code describes the dimensions issue at particular instances inside the animation. Within the preview canvas, you’ll discover a smoother and extra fluid animation for the emoji icon.

swiftui-keyframeanimator-scale-smoothly

A number of Keyframe Tracks

Up till now, we have now centered on a single keyframe monitor to change the dimensions issue. Nevertheless, keyframes present the power to animate a number of results independently by defining separate tracks, every with its personal distinctive timing. By incorporating a number of tracks, we are able to concurrently animate numerous properties, enabling us to create extra superior animations.

In the identical demo, we are able to outline separate keyframe tracks for the vertical stretch, translation, and opacity properties. Right here is the pattern code:

By using a number of keyframe tracks, we are able to obtain an intriguing animation impact. On this case, the emoji icon will transfer round randomly, whereas its opacity varies at particular time limits.

swiftui-keyframeanimator-move-around

By default, keyframe animator retains operating the animation repeatedly. If you wish to cease the animation, you’ll be able to set the repeat parameter to false.

You need to use ZStack and overlay one other emoji icon to create animation as proven beneath. I’ll go away this as an train so that you can discover and implement.

swiftui-keyframeanimator-two-emojis

Abstract

KeyframeAnimator is a helpful characteristic launched in iOS 17. In distinction to the PhaseAnimator modifier, this new device in SwiftUI empowers builders to create superior animations utilizing keyframes.

By leveraging keyframes, builders can outline particular time limits and exactly manipulate the properties of their animations. This enhanced management permits for the creation of intricate and dynamic visible results, leading to a extra fluid animations.

For those who discover this tutorial helpful, don’t neglect to take a look at our Mastering SwiftUI e-book to dive deeper into SwiftUI.



Leave a Reply

Your email address will not be published. Required fields are marked *