SwiftUI - GeometryReader overrides custom transition

344 Views Asked by At

I want to apply a custom transition on a set of views enclosed by a GeometryReader, as shown below:

(✔) Custom transition works:

    var body: some View {
    ZStack {
        VStack {
            VStack {
                ForEach(modelView.views) { view in
                    GeometryReader(content: { geometry in
                        RoundedRectangle(cornerRadius: 25.0)
                    })
                    .transition(.myCustomTransition) 
                    // Transition declared on the enclosing GeometryReader
                    // Custom transition works
                }
            }
        }
    }

(✗) Custom transition is ignored, gets defaulted to .opacity:

    var body: some View {
    ZStack {
        VStack {
            VStack {
                ForEach(modelView.views) { view in
                    GeometryReader(content: { geometry in
                        RoundedRectangle(cornerRadius: 25.0)
                            .transition(.myCustomTransition)
                        // Transition declared on the RoundedRectangle, as intended
                        // Custom transition gets replaced by an .opacity transition
                    })
                }
            }
        }
    }

Custom transitions on the RoundedRectangles work OK in the first block of code above where it's declared on the enclosing GeometryReader, but I can't structure it like this as I need the RoundedRectangles' current positioning data -which I have to get from the geometry var- to feed as an input to the custom transition. (This part was omitted from the above code for brevity, think of it like .myCustomTransition(param: aRequiredValueFromTheGeometryProxy)) So I need the transition to be declared inside the said GeometryReader like on the second example.

I cannot come up with any reason why the transitions on the second example wouldn't work as intended. I'm in no way an expert on SwiftUI, so I'm pretty sure I'm getting something wrong here.

Any ideas on why this happens and any possible workarounds would be greatly appreciated!

P.S. Rather unsurprisingly, adding an .transition(.identity) on the enclosing GeometryReader didn't work.

P.P.S. I did not include my custom transition implementation as it seems to be inconsequential to the problem. (i.e. When I replace the transition value with .slide, that doesn't work either)

0

There are 0 best solutions below