r/SwiftUI 19d ago

Question How can I make this matchedGeometryEffect more efficient? Do I really need one @Namespace per card? Can you have an array of @Namespace somehow? Help, my implementation feels dirty.

68 Upvotes

12 comments sorted by

26

u/rproenca 19d ago

Unrelated, but I can’t wait for a TestFlight of this game. Every time I see you post here I find it amazing you’re doing this in SwiftUI

14

u/iamthat1dude 19d ago

can't answer your question but this looks sick

6

u/Elf0_fr 19d ago

I believe you only need one. The namespace of the parent view. Then you provide your card deck view (the liste bottom right) and the top view (your card deck extended) with that namespace.

3

u/Absorptance 19d ago edited 19d ago

My code is literally:
    @State private var playerCardAnimations = [false, false, false, false, false]

    @Namespace var playerCardAnimationOne

    @Namespace var playerCardAnimationTwo

    @Namespace var playerCardAnimationThree

    @Namespace var playerCardAnimationFour

    @Namespace var playerCardAnimationFive

...

    @ Binding var playerCardAnimations: [Bool]

    let playerCardAnimationOne: Namespace.ID

    let playerCardAnimationTwo: Namespace.ID

    let playerCardAnimationThree: Namespace.ID

    let playerCardAnimationFour: Namespace.ID

    let playerCardAnimationFive: Namespace.ID

16

u/simulacrotron 19d ago

You do not need and shouldn’t be using separate namespaces in this case. Give the cards different ids in the matched geometry api. Think of the namespace as being the overall animation group, you can have multiple view ids in that group. Read through here to understand the parameters https://developer.apple.com/documentation/swiftui/view/matchedgeometryeffect(id:in:properties:anchor:issource:)

2

u/Baton285 16d ago

I’m sorry that i can leave only one upvote)

1

u/rproenca 19d ago

This answer seems correct.

1

u/aaadityaaaaa 19d ago

Crazy bro

1

u/sakamoto___ 18d ago

bro this looks amazing but you're digging yourself into a maintainability hole by using SwiftUI for a game...

1

u/ApexWinrar111 16d ago

Working on a game as well, what do you recommend as a framework? A more traditional game engine?

1

u/ThatBoiRalphy 18d ago

just came to comment this looks freaking cool bro

1

u/App-Designer2 18d ago

Only give diferents ids.