动画不适用于 GeometryReader 和 NavigationView 的组合

Animation not working with combination of GeometryReader and NavigationView

我有一个使用偏移量和计时器上下滑动的动画图像。在您组合 GeometryReader 和 NavigationView 之前,这完全可以正常工作。就 NavView 和 GeoReader 而言,动画效果也很好。任何解决方案? (我知道,在这个例子中不需要 GeometryReader)

struct TestView: View{

    @State var offsetSwipeUp: CGFloat = 0
    
    var body: some View{
        let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
        return NavigationView {
            GeometryReader { geometry in
                Image(systemName: "chevron.up")
                    .animation(.easeInOut(duration: 1))
                    .onReceive(timer){ _ in
                        if self.offsetSwipeUp == .zero{
                            self.offsetSwipeUp = -10
                        } else {
                            self.offsetSwipeUp = .zero
                        }
                }
                .offset(y: CGFloat(self.offsetSwipeUp))
                .navigationBarTitle("")
                .navigationBarHidden(true)
            }
        }
    }
}

在这种情况下,修饰符的顺序看起来很重要。

以下变体有效。使用 Xcode 11.4 / iOS 13.4

测试
struct TestView: View {

    @State var offsetSwipeUp: CGFloat = 0

    var body: some View{
        let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
        return NavigationView {
            GeometryReader { geometry in
                Image(systemName: "chevron.up")
                    .animation(.easeInOut(duration: 1))
                    .offset(y: CGFloat(self.offsetSwipeUp))
                    .navigationBarTitle("")
                    .navigationBarHidden(true)
            }
        }
        .onReceive(timer){ _ in
            if self.offsetSwipeUp == .zero{
                self.offsetSwipeUp = -10
            } else {
                self.offsetSwipeUp = .zero
            }
        }
    }
}