Swiftui 为什么文本在加载动画时出现?
我想在NavigationBarTitle后面隐藏一些文本,并在用户按下按钮时显示,它工作正常。唯一的问题是动画。在加载时,我们看到文本在导航Bartitle后面移动,这不是我想要的 我怎样才能解决这个问题 我尝试了偏移量和位置,但不起作用 代码:Swiftui 为什么文本在加载动画时出现?,swiftui,swiftui-animation,Swiftui,Swiftui Animation,我想在NavigationBarTitle后面隐藏一些文本,并在用户按下按钮时显示,它工作正常。唯一的问题是动画。在加载时,我们看到文本在导航Bartitle后面移动,这不是我想要的 我怎样才能解决这个问题 我尝试了偏移量和位置,但不起作用 代码: import SwiftUI struct TestZStackNavigationView: View { @State var isShowed = false let screenSize: CGRect = UIScreen
import SwiftUI
struct TestZStackNavigationView: View {
@State var isShowed = false
let screenSize: CGRect = UIScreen.main.bounds
var body: some View {
NavigationView {
VStack(alignment: .center, spacing: 0){
Text("Im the hidden text")
.fontWeight(.bold)
.foregroundColor(Color.white)
.frame(width: screenSize.width, height: 40, alignment: .center)
.background(Color.red)
// .offset(y: self.isShowed ? -315 : -355)
.position(x: screenSize.width / 2, y: self.isShowed ? 20 : -40)
.animation(.easeIn(duration: 0.5))
Button(action: {
self.isShowed.toggle()
}) {
Text("click me")
}
.navigationBarTitle(Text("Navigation Bar Title"), displayMode:.inline)
}
}
}
}
struct TestZStackNavigationView_Previews: PreviewProvider {
static var previews: some View {
TestZStackNavigationView()
}
}
有两种可能性
.animation(.easeIn(持续时间:0.5),值:isShowed)
.position(x:screenSize.width/2,y:self.isShowed?20:-40)
//.animation(.easeIn(持续时间:0.5))//从此处删除
按钮(操作:{
使用Animation(Animation.easeIn(duration:0.5)){/我现在想将它用于一个ObservedObject,但我得到了与第一个代码相同的行为。为什么
struct TestZStackNavigationView: View {
// @State var isShowed = false
let screenSize: CGRect = UIScreen.main.bounds
@ObservedObject var online = NetStatus()
var body: some View {
NavigationView {
VStack(alignment: .center, spacing: 0){
Text("Im the hidden text")
.fontWeight(.bold)
.foregroundColor(Color.white)
.frame(width: screenSize.width, height: 40, alignment: .center)
.background(Color.red)
.position(x: screenSize.width / 2, y: online.connected ? -40 : 20)
.animation(.easeIn(duration: 0.5), value: online.connected)
.navigationBarTitle(Text("Navigation Bar Title"), displayMode:.inline)
}
}
}
}没有动画,但为什么我们在加载时看到文本在其位置移动。。。?
struct TestZStackNavigationView: View {
// @State var isShowed = false
let screenSize: CGRect = UIScreen.main.bounds
@ObservedObject var online = NetStatus()
var body: some View {
NavigationView {
VStack(alignment: .center, spacing: 0){
Text("Im the hidden text")
.fontWeight(.bold)
.foregroundColor(Color.white)
.frame(width: screenSize.width, height: 40, alignment: .center)
.background(Color.red)
.position(x: screenSize.width / 2, y: online.connected ? -40 : 20)
.animation(.easeIn(duration: 0.5), value: online.connected)
.navigationBarTitle(Text("Navigation Bar Title"), displayMode:.inline)
}
}
}