UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 46 - Animating Shapes with animatableData: Why is animatableData only sometimes necessary

Forums > 100 Days of SwiftUI

Hello,

Although it is clear that in we need to write an animatableData variable for SwiftUI to be able to interpolate between 2 different states in order to do a smooth animation, it is not clear to me why sometimes the animation can occur without doing this..?

My guess is that when you create a custom struct you need to include an animatableData variable, but when you use a built-in struct you do not (i.e. you do not have to write an extension anywhere).

I am pretty new and not very good at crawling through documentation, so if you have any resource tips via the apple developer site or through XCode that would be appricate!

Thanks!

2      

You are basically correct. When you use a struct that is already included in SwiftUI, such as RoundedRectangle, you do not need to add an animatableData property to the struct, because it already exists in that struct's definition.

However, when you define your own struct, you do need to include animatableData as a property if you want your struct to be animatable. (That is, if you want your struct to conform to the Animatable protocol.)

If you look at this documentation page for the RoundedRectangle struct, and scroll down to the "Instance Properties" section, you will see that it does have a property named animatableData.

https://developer.apple.com/documentation/swiftui/roundedrectangle

Also, if you look at this page and scroll down to the "Conforming Types" section, you will see all of the types included in SwiftUI that conform to the Animatable protocol. All of those types would have a property named animatableData included in them.

https://developer.apple.com/documentation/swiftui/animatable

3      

Thank you for that clarification!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.