簡體   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