问题描述
我正在Apple开发人员页面中尝试SwiftUI教程。现在,我正在关注过渡教程,但是添加视图时过渡无法正常工作。
这是我的代码。
vstack(alignment: .leading) {
HStack() {
// title
Text(titleText)
.font(.headline)
.padding()
Spacer()
// button
Button(action: {
withAnimation {
self.showDetail.toggle()
}
}) {
Image(systemName: "chevron.right.circle")
.imageScale(.large)
.rotationEffect(.degrees(showDetail ? 90 : 0))
.padding()
}
}
// detail
if showDetail {
Text(contentText)
.transition(.slide)
.padding()
}
}
我认为具有contentText的Text应该随幻灯片过渡出现,但是当我按下Button时,它会弹出。但是当我再次按下按钮时,它会随着过渡而消失。因此移转是可行的。
我该如何解决?
解决方法
经过测试,我找到了解决方案。
解决方案1: 这只是XCode画布的错误。 它可以在模拟器或真实计算机上工作。
解决方案2。(如果您真的想在画布上做)
Button (action : {
withAnimation(.easeInOut) { // add animation manually
self.showDetail.toggle()
}
}) {
// label Image no change
}
if showDetail {
Text(contentText)
.transition(.slide)
.animation(.easeInOut) // match with withAnimation
}
,
尽管我还没有完成您正在谈论的教程,但是我认为使用.move过渡而不是.slide过渡可以正常工作。 这是细节部分的代码(其余部分保持不变)。希望我的回答对您有帮助
if showDetail {
Text(contentText)
.transition(.move(edge: .leading))
.padding()
}