1

I've got an animated Image which is sliding up and down, using the offset and a timer. This works totally fine until you combine a GeometryReader and a NavigationView. For both, NavView and GeoReader on their own, the animation is working as well. Any solutions? (I know, in this example the GeometryReader is not needed)

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)
            }
        }
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
adelmachris
  • 332
  • 2
  • 13

1 Answers1

0

In this case order of modifiers looks important.

The below variant works. Tested with 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
            }
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690