SwiftUI ScrollView自动添加不需要的动画

问题描述 投票:0回答:1

我在SwiftUI ScrollView中的动画有问题。我可以使用下面的代码在Playground中重现它。我只想为不透明度设置动画,但它也可以缩放动画。如果我使用VStack而不是ScrollView,则可以使用。但是我需要它可以滚动。

有人遇到过同样的问题,可以给我快速提示吗?

实际行为:https://giphy.com/gifs/h8DSbS1xZ9PJyHIJrY

import SwiftUI
import PlaygroundSupport

struct ContentView: View {

    @State var showText = 0.0
    var body: some View {
        ScrollView {

        Text("Test")
            .font(.title)
            .opacity(showText)
        Text("Another really really long text")
            .opacity(showText)
        }
    .frame(width: 320, height: 420)
    .background(Color.red)
    .onAppear {
        withAnimation(Animation.easeInOut(duration: 1)) {
            self.showText = 1.0
        }
        }
    }
}

PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
animation swiftui scrollview
1个回答
0
投票

这里是可能的解决方案。经过Xcode 11.4 / iOS 13.4的测试]

ScrollView {
    VStack {
        Text("Test")
            .font(.title)
        Text("Another really really long text")
    }
    .fixedSize()
    .opacity(showText)
}
© www.soinside.com 2019 - 2024. All rights reserved.