【问题标题】:Animation not working with combination of GeometryReader and NavigationView动画不适用于 GeometryReader 和 NavigationView 的组合
【发布时间】:2020-04-29 11:42:12
【问题描述】:

我有一个使用偏移量和计时器上下滑动的动画图像。在您结合 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)
            }
        }
    }
}

【问题讨论】:

    标签: animation swiftui navigationview geometryreader


    【解决方案1】:

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

    以下变体有效。使用 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
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多