Keyframe Animation In Swift
In the beautiful world of modern iOS applications, not much stands out more than well applied animations and transitions. They capture the user’s attention and set your app apart from the competition. If you’ve ever wished to add cool animations to your current or future projects but felt it was too challenging, this article is purposely meant to change your mind.
I have already set up a small project for us to work on. You can download it here or if you prefer to check out the GitHub repo, you can do that as well.
Once you’ve downloaded or cloned it, go ahead and open the project file. You should find a file structure that looks like this:
Ios Animations By Tutorials, Chapter 17: Stroke & Path Animations
You’ll see a single view controller with a simple maroon background, a blue UIView at the centre of the scene and an Animate button at the bottom. We will be using this view controller to demonstrate animating views using keyframes.
This is a very exciting part of iOS animations that I enjoy working with. If you’ve done video-editing or something similar, you might have an idea of what keyframe animations entail. However, if you have no idea of what to expect, don’t worry, I will explain the concept in a simple way.
Do. How about we make it move left, then up, then right, then down and finally back to its original position? Sounds great? Awesome, let’s do that.
How To Create Swift Animations With Ease
The first movement will be towards the left. It will have a relative start time of 0 and let’s give it a relative duration of 0.125. Let’s also make the view a bit smaller as well while we’re at it.
The good thing about keyframe animations is that each individual keyframe is independent so you can always run it at a specific point and see if you like the animation before adding another.
The next movement we want to achieve is upwards so add another keyframe, right after the closing parenthesis of the previous keyframe, to handle that.
Keyframe Animations In Swiftui
Notice the relative start time of the second keyframe. It starts at the end of the first one and goes on for a quarter of the total animation duration.
Quick Tip: You can overlap keyframes, if you so wish, however, do not animate changes to the same property on two overlapping keyframes.
That’s just the basics on keyframe animations. It barely scratches the surface of what you can do. You can combine keyframes animations to perform powerful and more complex animation curves.
Delightful Interactive Animations · On Swift Wings
Go ahead and experiment with keyframes, like adding an overlapping keyframe that changes the color or the alpha of the view or whatever you would like. You can also add animations options in the
If you would like to explore more about iOS animations, I would suggest you have a look at my other posts on Constraint Animations, View Animations and Animating View Properties.Keyframe animations are the a special sort of view animation that, instead of going from point A to point B, let you create animations with multiple milestones. Keyframes are another building block of complex, eye pleasing animations which allow you to upgrade from simple one-shot animations to designing complex animations sequences.
Let’s see how it would look like if you wanted to chain multiple simple animations together and move a view in a rectangular pattern:
What Are Keyframe Animations In Swift 5?
Look at all that nesting! And that’s for a simple rectangle-shaped path; can you imagine what that would look like for a more complicated movement?
Instead, you can split the total animation into four distinct stages, or keyframes, and then combine the individual keyframes into a keyframe animation.
Consider another animation that depicts the take-off sequence of a plane. The distinct stages of this animation would look like the following:
Exploring Shake Animation With Swift 3
The first stage of the animation sequence accelerates the plane on the runway. The second stage gives the plane a little altitude and tilts it upward. The third stage continues to tilt the plane and boosts the plane skyward at a much faster rate.
There’s a fourth and final stage in the final 10% of the animation that fades the plane out of view, as if it were moving behind some low-hanging clouds.
The complete animation could be overwhelming to create, but breaking down the animation into the various stages makes it a lot more manageable. Once you’ve defined the keyframes for each stage, you’ve got the problem mostly solved.
Animating View Properties In Xcode With Swift 4 And Auto Layout
You’ll learn about keyframes and how to assemble them into a keyframe animation by creating an airplane animation similar to the above — but it will be far more complex. Don’t worry, you’re more than capable of handling this!
If you worked through the project in the last chapter, you can use that as your starting point; if you didn’t do that, or if you want to start fresh, you can use the starter project for this chapter.
You’ll make the airplane take off from its starting position, circle around, then land and taxi back to the starting point. This animation will run each time the screen switches between connecting flights.
Animations In Swiftui With Examples — Part 3
To create your first keyframe animation. The parameter list is identical to the ones you’ve used all along in this book to create your view animations. You set the duration to
Is the first time you’ve used relative durations in the animations in this book. The start time of the keyframe, as well as its duration, are expressed as percentages relative to the entire duration of the animation. For example,
Working with relative values lets you specify that a keyframe should last for a fraction of the total time; UIKit takes the relative durations of each keyframe and automagically figures out the exact durations for each keyframe, saving you a ton of work.
Animations In Swift / Ios 8
Seconds. Later on, if you decide to modify the total duration of the animation to a different value, the individual keyframe animations will recalculate their duration times accordingly.
This is, of course, only the first step in creating the complete animation sequence. To create the second keyframe, add the following code to the keyframe block, underneath the previous
The second keyframe starts 10% of the way into the animation and lasts for 40% of the total duration. This keyframe rotates the airplane as it moves off the runway:
How To Add Lottie Animations In Ios Apps (swift)
The plane flies out of sight and disappears. Now you’ll need to bring it back and land it safely on the next connecting flight screen.
Before you show the airplane on the screen, you’ll have to reset the orientation of the plane — that is, undo the rotation you’ve applied — and move it to the left of the visible area.
This keyframe runs after all three previous frames have completed and lasts just a small fraction of a second; it resets the plane transform and moves it to the left edge of the screen.
Blender Tutorial: Animation
Take a minute to review the code you wrote. It’s easy to follow and decode; you can modify, rearrange or alter the timing of any of the sections above without too much work on your part.
It’s a short step to imagine your animation as a series of separate animations with delays in between, or even as a set of animations triggered from completion closures. Keyframes are an incredibly useful and flexible way to design and control your animations.
Keyframe animations don’t support the built-in easing curves that are available in standard view animations. This is by design; keyframes are supposed to start and end at specific times and flow into each other.
Adding Spring Animations
If each stage of your animation above had an easing curve, the plane would jerk around instead of moving smoothly from one animation into the next. If you
Apply easing to the entire animation, that would result in your animation durations being ignored — and that’s not what you want.
Instead, you have several calculation modes to choose from; each mode offers a different method to calculate intermediate frames of the animation as well as different optimizers for smooth movement and even pacing. Check out the documentation by searching for
Ios Animations By Tutorials: Ios 10 And Swift, 3rd Edition #enprogbooks@iamdev ***************..
Now that you know how to group together any number of simple animations using keyframe animations, you can build just about any sequence that comes to mind. If you want to test your knowledge of keyframes and keyframe animations, give the challenge below a try before moving on to the next section.
There’s still one UI element on screen that isn’t animated: the black flight summary status bar at the top of the screen that shows the flight departure times.
Your challenge is to add a keyframe animation to move the summary offscreen each time the connecting flight data changes. Then you’ll change the text to reflect the departure time of the next flight animate it back on screen like so:
Advanced Animation In Swiftui Under Ios17
The next section of this book delves into the seemingly complex world of Auto Layout and animations; you’ll learn that Auto Layout isn’t nearly as mystifying as you might have thought, and you’ll learn to animate constraints just as easily as you animate views!
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
Posting Komentar untuk "Keyframe Animation In Swift"