Easy methods to Create an Animated Navigation Menu in SwiftUI

[ad_1]

One cause why I actually take pleasure in programming utilizing the SwiftUI framework is that it makes really easy to animate view modifications. Particularly, the introduction of the matchedGeometryEffect modifier, launched in iOS 14, additional simplifies the implementation of view animations. With matchedGeometryEffect, all you want is describe the looks of two views. The modifier will then compute the distinction between these two views and robotically animates the scale/place modifications.

We have now written a detailed tutorial on matchedGeometryEffect. I extremely suggest you to test it out if that is the very first time you come throughout this modifier. On this tutorial, we’ll make use of matchedGeometryEffect to develop an animated navigation menu just like the one proven under.

swiftui-animated-navigation-menu-matchedGeometryEffect

Editor’s Be aware: To dive deeper into SwiftUI animation and study extra concerning the SwiftUI framework, you’ll be able to try the guide here.

Creating the Navigation Menu

Earlier than we create the animated menu, let’s begin by creating the static model. For instance, the navigation menu solely shows three menu gadgets.

navigation-menu-swiftui

To format three textual content views horizontally with equal spacing, we use the HStack view and Spacer to rearrange the views. Right here is the code pattern:

As you’ll be able to see from the code above, it comprises numerous duplications. It may be additional simplified with ForEach:

We added a state variable named selectedIndex to maintain observe of the chosen menu merchandise. When the menu merchandise is chosen, we spotlight it in purple. In any other case, its background coloration is about to mild grey.

To detect customers’ contact, we hooked up the .onTapGesture modifier to the textual content view. When it’s tapped, we replace the worth of selectedIndex to spotlight the chosen textual content view.

Animating the Navigation Menu

Now that we’ve carried out the navigation menu, nevertheless, it misses the required animation. To animating the view change each time a menu merchandise is chosen, all we have to do is create a namespace variable and fix the matchedGeometryEffect modifier to the textual content view in purple:

The ID and namespace are used for figuring out which views are a part of the identical transition. We additionally want to connect the .animation modifier to the HStack view to allow the view animation. Be aware that this mission is constructed utilizing Xcode 13. The animation modifier is up to date within the new model of iOS. You need to present the worth to watch for modifications. Right here, it’s the selectedIndex.

When you made the modifications, you’ll be able to take a look at the NavigationMenu view in a simulator. Faucet a menu merchandise and you will note a pleasant animation when the merchandise is transited from one state to a different.

animated-swiftui-navigation-menu

Utilizing the Animated Navigation Menu View

To use this animated navigation menu to your mission, you’ll be able to modify the NavigationMenu view to simply accept a binding to the chosen index:

For instance, you’ve gotten created a page-based tab view like this:

You’ll be able to add the NavigationMenu view as an overlay and use your individual menu gadgets.

swiftui-tab-view-animated-tab-bar


Founding father of AppCoda. Creator of a number of iOS programming books together with Starting iOS Programming with Swift and Mastering SwiftUI. iOS App Developer and Blogger. Comply with me at Fb, Twitter and Google+.



[ad_2]

Leave a Comment