ScrollView中的动画不起作用?使用Xcode 11 beta 6 [英] Animation in ScrollView is not working ? Using Xcode 11 beta 6
问题描述
我试图制作ScrollView过渡动画,但发现在scrollView中它们的工作方式有所不同。但是仍然无法在其中做动画。我正在提供代码,请看一看。使用Xcode 11 beta 6
I was trying to to do transition animation Scrollview but found out that how things work differently in scrollView. But still unable to do animation in it. I am providing code, please have a look. Using Xcode 11 beta 6
import SwiftUI
struct ContentView : View {
@State private var isButtonVisible = false
var body: some View {
NavigationView {
ScrollView{
VStack {
Button(action: {
// withAnimation {
self.isButtonVisible.toggle()
// }
}) {
Text("Press me")
}
if isButtonVisible {
Text("sss")
.frame(height: true ? 50 : 0, alignment: .center)
.background(Color.red)
.animation(.linear(duration: 2))
// .transition(.move(edge: .top))
}
}
}
}
}}
推荐答案
这肯定是一个错误,我建议您向Apple提交错误报告。我找到了一种解决方法(请参见下面的代码),但是不幸的是它发现了另一个错误!
This must be a bug, and I suggest you file a bug report with Apple. I find a workaround (see code below), but it unfortunately uncovers another bug!
为了使ScrollView中的动画正常工作,您可以将内容封装在自定义视图。
In order to make the animation inside the ScrollView to work, you can encapsulate the contents in a custom view. That will fix that problem.
这将发现一个新问题,这可以从我添加到代码中的边框中看出:添加文本视图时,它会移动
This will uncover a new issue, which is made evident by the borders I added to your code: when the Text view is added, it shifts parts of the contents outside the ScrollView.
您会发现这是不正确的。尝试使用默认值isButtonVisible = true启动您的应用程序,您会看到它以不同的方式呈现。
You will see that this is not correct. Try starting your app with a default value isButtonVisible = true, and you will see it renders it differently.
struct ContentView : View {
var body: some View {
NavigationView {
ScrollView {
EncapsulatedView().border(Color.green)
}.border(Color.blue)
}
}
}
struct EncapsulatedView: View {
@State private var isButtonVisible = false
var body: some View {
VStack {
Text("Filler")
Button(action: {
withAnimation(.easeInOut(duration: 2.0)) {
self.isButtonVisible.toggle()
}
}) {
Text("Press me")
}
if isButtonVisible {
Text("sss")
.frame(height: true ? 50 : 0, alignment: .center)
.transition(.opacity)
.background(Color.red)
}
Spacer()
}.border(Color.red)
}
}
这篇关于ScrollView中的动画不起作用?使用Xcode 11 beta 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!