Hello and welcome to another blog post about SwiftUI. This years WWDC is approaching fast and the expectation of new SwiftUI features and/or changes is tremendous. So I have decided to dedicate this post to something lighter yet still playful.
I will share with you a simple way of creating a TikTok logo-ish effect but the main takeaway of this article is meant to be understanding how to create reusable styling for your app.
Start with blending of layers
The key to creating our logo is in specific blending of two colored shapes. Please check the code first:
As you see, I have used a simple SF Symbol as our logo. The resulting view takes this image, fills it with custom red color (defined in project assets) and as
.overlay sets the same logo in blue, but slightly shifted with
.offset viewModifier. The white area is the result of blend mode that is set to
Take this example and try to experiment with the viewModifier parameters, especially
.blendMode to see how it affects the result. For instance, if you wish to use your logo on white background, you may want to set it to
Wrap the effect as custom ViewModifier
We have achieved our logo with effect quite easily, but what if we need to apply the same effect to more elements in our app?
The beauty of SwiftUI is that you can specify custom ViewModifier and hide almost any of your styling into its implementation and thus gain the ability to reuse it anywhere.
My implementation looks like this:
You can see that the overall structure derives from our first example, but it is no longer dependent on specific Image. Instead, the view modifier takes the
Content that it has been applied to and sets the same content also as its overlay (but with a different color).
Also, the offset is prepared to be set as viewModifier parameter so it can be changed from the outside.
I have also introduced a negative offset here, that shifts the base content in the opposite direction to its overlay. With this improvement, the resulting view remains centered which I personaly prefer but it is optional change.
Our view modifier can be used like this to any kind of view, even the root view if you wish;)
Reuse view modifier within ButtonStyle
Custom view modifiers are especially handy when creating custom ButtonStyle, transitions, or animations. Let me share here an example of a button, that applies our effect when pressed:
Here we are changing the offset value of our effect based on
configuration.isPressed value. The resulting button created as:
now behaves like this:
- Styling the app and creating reusable styles can benefit heavily from custom view modifiers. This is only one of the many use-cases, but the same approach can be used for any app look and feel
- It is wise to keep configurable interface of your view modifier so it can be tuned for specific scenarios and foremost - ANIMATIONS. As an inspiration and your challenge, I present you a loading indicator based on our TikTokEffect. Can you achieve the same - or better?
Did you like this article? What do you want me to focus on next?
Feel free to comment or criticize so the next one is even better. Or share it with other SwiftUI adopters ;)