Close Menu
geekfence.comgeekfence.com
    What's Hot

    ClickFix attackers using new tactic to evade detection, says Microsoft – Computerworld

    March 7, 2026

    M&A Monthly: February/March 2026

    March 7, 2026

    Posit AI Blog: luz 0.4.0

    March 7, 2026
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»iOS Development»Using Navigation Transition to Create Hero Animation in iOS 18
    iOS Development

    Using Navigation Transition to Create Hero Animation in iOS 18

    AdminBy AdminDecember 31, 2025No Comments4 Mins Read4 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    Using Navigation Transition to Create Hero Animation in iOS 18
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Apple’s engineers may have long recognized the widespread desire among iOS developers to recreate the elegant hero animation featured in the App Store app. Understanding the complexity and time investment typically required to implement such animations from scratch, Apple has incorporated this feature into the iOS 18 SDK.

    With this update, you can now achieve a similar animated transition effect in your own apps using just a few lines of code. This significant enhancement empowers developers to create visually appealing and seamless transitions, elevating the overall user experience of their apps.

    In this tutorial, we’ll explore how to leverage the new NavigationTransition protocol and the matchedTransitionSource modifier to create hero animations during view transitions.

    The Simple Demo App

    Let’s dive into a demo app to explore the new APIs. We’ll begin with a simple app that shows a list of cafes in a standard scroll view. Our goal is to implement a feature where tapping on a cafe takes you to a new screen displaying a full image with a hero animation.

    Using Navigation Transition Protocol

    To display a full image and animate the view transition using hero animations, you can use the following steps:

    1. Embed the scroll view within a navigation stack.
    2. Use NavigationLink to enable tapping on the card view.
    3. Declare a namespace with @Namespace to support the hero animation.
    4. Attach the matchedTransitionSource modifier to the excerpt mode card view.
    5. Attach the navigationTransition modifier to the full content mode card view.

    By completing these steps, SwiftUI will automatically generate a smooth hero animation, expanding the selected cafe item into a full-screen image when tapped.

    Creating Hero Animations for View Transitions

    Now we’ll modify the project to support navigation. To start, let’s embed the scroll view within a navigation stack, as shown below:

    NavigationStack {
    	ScrollView {
    	
    		// Existing code
    		
    	}
    }

    Next, create the detail view for displaying the full image like below. It accepts a cafe object as input and displays its image in a full-screen view.

    struct DetailView: View {
        var cafe: Cafe
        @Environment(\.dismiss) var dismiss
        
        var body: some View {
            Image(cafe.image)
                .resizable()
                .scaledToFill()
                .frame(minWidth: 0, maxWidth: .infinity)
                .clipped()
                .overlay(alignment: .topTrailing) {
                    Button {
                        dismiss()
                    } label: {
                        Image(systemName: "xmark.circle.fill")
                            .font(.system(size: 30))
                            .foregroundStyle(Color.white)
                            .opacity(0.7)
                            .padding()
                            .padding(.top, 30)
                    }
                }
                .ignoresSafeArea()
        }
    }

    To enable interaction with the cafe photos, we can use NavigationLink to manage the navigation. When tapped, the app displays the detail view which shows the image in full screen view.

    ForEach(sampleCafes) { cafe in
        
        NavigationLink {
            DetailView(cafe: cafe)
            
        } label: {
            Image(cafe.image)
                .resizable()
                .scaledToFill()
                .frame(minWidth: 0, maxWidth: .infinity)
                .frame(height: 400)
                .clipShape(RoundedRectangle(cornerRadius: 20))
        }
        .padding()
    }

    In preview mode, you can navigate between the detail view and the list view. At this stage, the transition uses the default animation for navigation stacks, without any custom effects.

    Now it comes to the fun part. Let’s create the hero animation by using the new NavigationTransition protocol. The very first step is to define a namespace for the animation. In ContentView, declare the following namespace variable:

    @Namespace var namespace

    Next, apply the matchedTransitionSource modifier to the source view, which is the image view in the list. Then, use the navigationTransition modifier on the detail view. Update your code as shown below:

    NavigationLink {
        DetailView(cafe: cafe)
            .navigationTransition(.zoom(sourceID: cafe.id, in: namespace))
            .toolbarVisibility(.hidden, for: .navigationBar)
        
    } label: {
        Image(cafe.image)
            .resizable()
            .scaledToFill()
            .frame(minWidth: 0, maxWidth: .infinity)
            .frame(height: 400)
            .clipShape(RoundedRectangle(cornerRadius: 20))
            .matchedTransitionSource(id: cafe.id, in: namespace)
    }

    To enhance the visual experience, I’ve included the toolbarVisibility modifier to conceal the navigation bar. This removes the Back button from view, creating a more immersive full-screen presentation of the cafe image.

    In preview mode, test the app by tapping a cafe image. It will display a full-screen image with a hero animation. To return to the list, either tap the “X” button or simply drag the image downwards. The app will animate the image back to its original position in the list, providing a fluid and intuitive user experience.

    Summary

    The new NavigationTransition protocol has made it remarkably simple for developers to create hero animations for view transitions, allowing for a richer user experience with just a few lines of code. Consider exploring this new feature to elevate your app’s interactivity and user satisfaction.

    It’s important to note that this API is only compatible with iOS 18 and later. If your app needs to support older iOS versions, you’ll have to implement the animation yourself. Our “Mastering SwiftUI” book provides guidance on how to achieve this.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    I wish I could be an OpenClaw Maintainer

    March 7, 2026

    Swift factory method design pattern

    March 6, 2026

    react native – How to solve “”xcodebuild” exited with error code 65.“ – error from npx expo run:ios?

    March 5, 2026

    Post | Cocoanetics

    March 1, 2026

    Swift abstract factory design pattern

    February 28, 2026

    Flutter: Force portrait UI on tablets (iOS + Android) even when device is landscape, but avoid letterboxing / black bars

    February 27, 2026
    Top Posts

    Hard-braking events as indicators of road segment crash risk

    January 14, 202619 Views

    Understanding U-Net Architecture in Deep Learning

    November 25, 202518 Views

    How to integrate a graph database into your RAG pipeline

    February 8, 202610 Views
    Don't Miss

    ClickFix attackers using new tactic to evade detection, says Microsoft – Computerworld

    March 7, 2026

    “And all Windows computers should already be restricted so that random, unsigned (not signed by…

    M&A Monthly: February/March 2026

    March 7, 2026

    Posit AI Blog: luz 0.4.0

    March 7, 2026

    Top Reasons to Choose Precisely for SAP and Salesforce Process Automation

    March 7, 2026
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    ClickFix attackers using new tactic to evade detection, says Microsoft – Computerworld

    March 7, 2026

    M&A Monthly: February/March 2026

    March 7, 2026

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2026 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.