3D Cube Loading Animation Using SwiftUI
Tags: swift, ios
Published: 2020-07-20 15:39:47

I recently came across [this](https://codemyui.com/lego-style-loading-animation/) lego loading animation and wanted to see if this can be created using SwiftUI. Let’s get started. 

## Breakdown

1. We have 5 3D cubes of different colours.
2. They are arranged to form a cuboid with space for one cube being empty. 
3. One by one the cube behind the empty space moves forward.
4. There are two scenarios in which two cubes move forward at once.

## Build

Let’s start with creating a simple 3d cube. For this, I downloaded a png image of cube and masked it over a colour. Below is the origin image and how it looks after the mask.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kdfoq7q7ptxynapqiogx.png)

Pretty cool. Now we can create a cube of any colour by just changing the colour of the base view. And if you took about the code it’s only about 10 lines. That’s why you got to love SwiftUI.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6qul9prvzjxfz5atn8fc.png)


![Code](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd70affab-024b-4bde-93c4-72e50cb237bc_1170x356.png)

Next we need to arrange them to form a cuboid. This is where we use the elementary maths that we all learnt (although we don’t remember most of it 😅). After some trial and error we have the co-ordinates for the 6 positions that our cubes should be in. 

(-80,40)

(-40,20)

(0,0)

(40,20)

(0,40)

(-40,60)

Once we have our co-ordinates, we can simply assign them to the cubes using .offset modifier. We use .offset because we only are about relative position between the cubes and it also does not get affected by the actual position of the cubes on the screen.

We also need decide the zindex of the cubes corresponding to each of these co-ordinates. After some elementary maths, we have the final values - 

(-80,40) - 5

(-40,20) - 3

(0,0) - 1

(40,20) - 2

(0,40) - 4

(-40,60) - 6

With the zIndex in place, our cubes finally sit nicely together. Look at that!

![Alt Text](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe4322d-d3aa-4322-8e7e-31ac91e58895_530x378.png)

----

![Alt Text](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd31fd3a2-64d9-4101-a0da-b9519881ee75_1110x1928.png)


In the above code section, you get a look into how the code is arranged. We have a enum with 6 elements. Each of these elements define their view. LegoView with a unique colour for 5 elements and a EmptyView for the 6th element.

We create a @State variable to store the co-ordinates and indices we earlier defined. We will talk about the 4th boolean parameter later. We also have a variable called currentIndex which denotes the index of the cube which will move next. The empty space will always be at the position currentIndex+1. 

## Animation

Now to the part you have been waiting for.  How do we animate these cubes. The offset of a cube is derived from the allIndices array. So if we change the allIndices array, the offset of our cube will get changed. So, all we need to do is swap the position of empty space with the position of cube next to empty space. The empty space is indexed 5 (as it’s the last element in the enum). 

![Alt Text](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6d7eacb7-ae03-4de8-ac5a-4e2e808780f1_674x358.png)

By swapping the offset, we make sure that the cubes move to their new position. TO animate this movement we simply need to perform the swap inside a withAnimation block. 

![Alt Text](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7ee3f53-5e8a-47a9-b3fe-ee5c52c2e19a_320x114.png)

To make sure that this animation continues forever, we call this function recursively. But since, we want some delay between each movement we use DispatchQueue.main.asyncAfter. 


![Alt Text](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0248e1c1-2b64-4d1f-8e01-f06745b97d1e_878x184.png)


We are almost there. According to the 4th point of our breakdown, there are 2 conditions in which 2 cubes move forward at once. These two scenarios are represented by the 4th Bool parameter in allIndices


![Alt Text](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5642d979-456b-42c5-bac1-b1a056d708d2_530x390.gif)

And that’s a wrap. Look at the cubes go! View the code on Github - https://github.com/vijaywargiya/LegoLoadingAnimation-SwiftUI