我在 ObservedObject 中有一個狀態變數,它決定了我在 SwiftUI 中顯示的兩個自定義視圖中的哪一個。
我在不同的地方弄亂了 .animation(.easeIn) 并嘗試了 .withAnimation() 的東西,但是在試驗時除了 XCode 投訴之外我什么也做不了。無論我把 .animation() 放在哪里,我都會得到一個編譯錯誤,當我運行代碼時沒有任何反應。當我觸發狀態更改時,只需從一個視圖輕彈到另一個視圖。
struct EventEditorView : View { /* SwiftUI based View */
var eventEditorVC : EventEditorVC!
@ObservedObject var eventEditorDataModel: EventEditorDataModel
var body: some View {
switch( eventEditorDataModel.editMode) {
case .edit:
EventEditModeView(eventEditorVC: eventEditorVC, eventEditorDataModel: eventEditorDataModel)
case .view:
EventViewModeView(eventEditorVC: eventEditorVC, eventEditorDataModel: eventEditorDataModel)
}
}
}
uj5u.com熱心網友回復:
您可以.transition在您的元素上使用 a ,并且withAnimation當您更改value影響其狀態的 時:
enum ViewToShow {
case one
case two
}
struct ContentView: View {
@State var viewToShow : ViewToShow = .one
var body: some View {
switch viewToShow {
case .one:
DetailView(title: "one", color: .red)
.transition(.opacity.combined(with: .move(edge: .leading)))
case .two:
DetailView(title: "two", color: .yellow)
.transition(.opacity.combined(with: .move(edge: .top)))
}
Button("Toggle") {
withAnimation {
viewToShow = viewToShow == .one ? .two : .one
}
}
}
}
struct DetailView : View {
var title: String
var color : Color
var body: some View {
Text(title)
.background(color)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459075.html
下一篇:如何為類使用懸停標簽?
