What's New in Animations in Swift 4 - Digital Leaves
2902
post-template-default,single,single-post,postid-2902,single-format-standard,ajax_fade,page_not_loaded,,select-theme-ver-3.8,wpb-js-composer js-comp-ver-5.1.1,vc_responsive
Advanced UIKit Animations in Swift.What's New in Animations in Swift 4 and iOS 11.

What’s New in Animations in Swift 4

The new iOS 11 has brought some nice additions and updates for animations in Swift 4. Let’s have a look at what’s new and some stuff that’s worth mentioning about advanced, modern animations currently.

If you have not dealt with animations in Swift before, I recommend you to have a look at this tutorial: Advanced UIKit Animations in Swift first.

New Animator Behaviors for Animations in Swift 4

There are two new properties for animators (UIViewPropertyAnimator) in Swift 4: Scrubs Linearly and and Pauses On Completion.

Scrubs Linearly

Imagine that you are building an interactive animation, so the user can interact with the view being animated by means of a gesture recognizer.

Probably, your code will be similar to this:

When the animation gets interrupted, it transitions to a linear curve by default. This is for efficiency reasons, to improve the response of the animation when being modified interactively, and for making the transition smoother when the animation continues (i.e: when the gesture recognizer ends its interaction).

Then, once the animation continues, the animator restores the original curve. Also, both remaining time and animation progress are recalculated.

New in Swift 4 and iOS 11, if you set the animator’s property scrubsLinearly to false, you will disable this curve transition. Thus, the animation will still behave with its original curve or spring behavior during the interaction. As a result, we can achieve some interesting effects.

What’s New In Animations In Swift 4

Additionally, it’s important to note that you can always specify a new curve when the animation resumes after the interaction has ended. Consequently, there are many options and effects you can explore when building your animations.

Pauses On Completion

Equally important is the new pausesOnCompletion property. By default, when an animation completes, it transitions to an inactive state. Hence, you can no longer modify or interact with the animation.

However,  now if you set the pausesOnCompletion property to true, the animation will stay paused but on an active state. As a result, you will be able to perform operations on the animation such as reversing or adjusting it and triggering it again.

What’s New In Animations In Swift 4

Nevertheless, there’s a caveat you must be aware of. As a consequence of the animation staying active, if you set pausesOnCompletion to true, the animation completion handler will not be called anymore. Instead, you can still be notified of the state of the animation by observing the running keypath of the animator.

Animations in Swift 4 Start as Paused

Previously, when you initialized an UIViewPropertyAnimator, you needed to specify its initial animations. Now, you can start it without any animations, and then use a block to add all the animations at once, so they all run immediately together. Additionally, you can add animations anytime. As a result, instead of escaping, they will run immediately.

Animatable Corner Radius And Partial Corners

Certainly one of the most interesting changes for animations, in my opinion, happened in the corner radius property of CALayer. As you may know, this property allows us to specify the corner radius of a view (concretely, of its layer), and is widely used to make round images, for example.

Now, for animations in Swift 4, this property is fully animatable. This is a big plus for animating the size of a circular view while keeping its rounded aspect.

Additionally, new in Swift 4, we have the possibility to round just some corners of the view. Up until now, I’ve been using a custom Swift class for that, so this is a nice addition.

You can do this thanks to the new property maskedCorners of CALayers, of type CACornerMask.

Some Thoughts On Animations

After the WWDC 2017, we’ve been left with some thoughts on modern, advanced animations for UIKit:

Best Practices For Interrupting Spring Animations

Due to limitations in how the spring animation curves work, and their states throughout the animation, spring animations always animate from their current state. This conflicts with the process of interrupting and continuing with an interactive animation.

Thus, if you need to interrupt a spring animation, these are just some tips and suggestions:

  • First, consider stopping the current animation, creating a new interactive one, and then re-creating a new animation.
  • Also, another option is using critically damped springs (i.e: those with low bouncing rate, dumping ratio 1 or close to 1).
  • Finally, another suggestion is decomposing the X and Y velocities of the animation in two separate animations.

Think Additively When Working With Animations

A very useful tip in my opinion. There are animations that will leave the item in the same state that it was initially. As an example, think of a 360 degrees rotation animation on a view. Because of how animations in UIKit are built, performing this animation directly will have no effect. The view won’t even move. Nothing, niente, nada.

In order to overcome this, you have to divide the animation in separate parts that DO modify the visual aspect of the view, and then chain them together.

Also, another option will be performing the animation with CoreAnimation (CAAnimation), but we of course loose some important high-level benefits like interactivity and scrubbing.

Thus, it’s always a nice idea to solve this situations with an additive approach. The additively animatable properties of UIView are:

 

  • transform
  • frame
  • bounds
  • center
  • position

Let’s see an example of a rotation performed in 100 steps of 𝛑/2.

Of course, you need to observe some efficiency and optimization aspects when building additive animations. Nevertheless, it’s a nice technique to apply in scenarios where a direct animation will not work properly.

In Conclusion

In this article, I described what’s new in animations in Swift 4 and the new iOS 11. Overall, nice additions and tweaks that help us have more control over our animations.

If you want to see some examples of animations in Swift 4, please refer to the tutorial Advanced UIKit Animations in Swift, or the sample code found here.

1 Comment
  • Leo

    July 23, 2017 at 4:58 am Reply

    The title seems to be misleading. I would better say what’s new in animations in UIKit because when I opened the web page, at first I thought what is the connection between Swift 4 language capabilities and animations…

Post a Comment

Before you continue...

Hi there! I created the Digital Tips List, the newsletter for Swift and iOS developers, to share my knowledge with you.


It features exclusive weekly tutorials on Swift and iOS development, Swift code snippets and the Digital Tip of the week.


Besides, If you join the list you'll receive the eBook: "Your First iOS App", that will teach you how to build your first iOS App in less than an hour with no prior Swift or iOS knowledge.

I hate spam, and I promise I'll keep your email address safe.