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

How to combine AnimatableModifier and matchedGeometryEffect?

Forums > SwiftUI

I am trying to combine two of the tutorials that are offered here on this site:

The first one explains how text sizes can be animated: https://www.hackingwithswift.com/quick-start/swiftui/how-to-animate-the-size-of-text

The second one explains how to syncronize animations betweens Views: https://www.hackingwithswift.com/quick-start/swiftui/how-to-synchronize-animations-from-one-view-to-another-with-matchedgeometryeffect

They both work perfectly independent of each other (as expected 😀), but when I try to combine the two to move and resize a Text from one View to another, it doesn't seem to work. I pass in the matchedGeometryEffect's NameSpace.ID to each View and everything then works fine with Circles and Rectangles, but not with Text. Of course, animating Text in itself requires an AnimatableModifier as described in the first link above. While that works ok in itself, it doesn't seem to work in combination with synchronized animations between Views (second link).

Here is some code that demonstrates the problem:

struct ContentView: View {
    @Namespace private var animation
    @State private var isLarge = false

    var body: some View {
        VStack {
            Text("Text that animates correctly")
                .offset(y: isLarge ? -100 : 0)
                .modifier(AnimatingFontSize(fontSize: isLarge ? 28 : 16))

            Button("Toggle") {
                withAnimation(.easeInOut(duration: 5.0)) {
                    isLarge.toggle()
                }
            }
            .padding(.vertical, 20)

            if isLarge {
                ViewWithText(animation: animation, color: .green, fontSize: 28, yOffset: 100)
            } else {
                ViewWithText(animation: animation, color: .red, fontSize: 16, yOffset: 0)
            }
        }
    }
}

struct ViewWithText: View {
    var animation: Namespace.ID
    var color: Color
    var fontSize: CGFloat
    var yOffset: CGFloat

    var body: some View {
        VStack {
            Circle()
                .matchedGeometryEffect(id: "Circle", in: animation)
                .foregroundColor(color)
                .frame(width: 50 + yOffset / 2, height: 50 + yOffset / 2)

            Text("Embedded Text")
                .foregroundColor(color)
                .matchedGeometryEffect(id: "Text", in: animation)
                .offset(y: yOffset)
                .modifier(AnimatingFontSize(fontSize: fontSize))
        }
    }
}

struct AnimatingFontSize: AnimatableModifier {
    var fontSize: CGFloat

    var animatableData: CGFloat {
        get { fontSize }
        set { fontSize = newValue }
    }

    func body(content: Self.Content) -> some View {
        content
            .font(.system(size: fontSize))
    }
}

The [Toggle] button toggles between large and small Texts and red and green Circles. The "Text that animates correctly" shows that the animatable font modifier works correctly. The ViewWithText contains a Circle and a Text. The Circles show that matchedGeometryEffect works correctly.

Alas, the "Embedded Text" in ViewWithText does not animate correctly: it fades in and out as if it were the default animation. I slowed down the animation so you can clearly see what happens. The AnimatingFontSize modfier does not seem to have any effect here.

Any ideas?

Thanks in advance!

2      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.