Mar*_* Jr 9 macos appkit swift swiftui
默认情况下,NSWindow如果/当视图发生变化时,托管 SwiftUI 视图的视图将自动调整大小以适应视图的大小。但是,如果更改是动画的,则窗口会跳转到最终大小而不使用动画。有没有办法使窗口大小与内容一起动画化?
我已经尝试过使用和不使用 SwiftUI 生命周期。我也尝试过调用该toggle方法NSAnimationContext.runAnimationGroup
struct ContentView: View {
@State var isExpanded = false
var body: some View {
VStack {
Button(isExpanded ? "Close" : "Open", action: toggleExpand)
if isExpanded {
ForEach(0..<5) { count in
Text("\(count). Some Item")
}
}
}
.padding()
.fixedSize()
}
func toggleExpand() {
withAnimation(.easeInOut(duration: 1)) { isExpanded.toggle() }
}
}
Run Code Online (Sandbox Code Playgroud)