繁体   English   中英

ScrollView 中的 SwiftUI animation 不工作

[英]SwiftUI animation within ScrollView not working

只是无法理解这一点。 我有一个简单的 animation 可以完美运行。 但是当我将视图包装到 ScrollView 中(通过取消注释 2 行)它不再动画了吗? 任何人的线索?

import SwiftUI

struct ContentView: View {
    @State var offset = CGSize(width: 0, height: 0)

    var body: some View {
//      ScrollView {
            VStack(spacing: 50) {
                Rectangle()
                    .frame(width: 100, height: 100)
                    .offset(self.offset)
                Button(action: {
                    withAnimation {
                        self.offset.width += 66
                    }
                })
                {
                    Text("Animate me")
                }
            }.frame(width: 300)
//      }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

我已经注意到了这种行为。 问题似乎是明确的 animation。 如果您对隐式 animation 使用 go 它可以工作:

struct ContentView: View {
    @State var offset = CGSize(width: 0, height: 0)

    var body: some View {
      ScrollView {
            VStack(spacing: 50) {
                Rectangle()
                    .frame(width: 100, height: 100)
                    .offset(self.offset)
                    .animation(.linear)
                Button(action: {
                    self.offset.width += 66
                })
                {
                    Text("Animate me")
                }
            }.frame(width: 300)
      }
    }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

我还没有设法得到原因,所以将其视为一种解决方法。 它可能是一个 SwiftUI 错误或我仍然无法理解的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM