首页 > 解决方案 > SwiftUI MultiviewResponder 神秘崩溃

问题描述

我遇到了这个我无法解释的崩溃,即使我将它归结为仍然会导致崩溃的最小组件:反复快速点击屏幕(使用两根手指)以隐藏/显示带有明确动画的覆盖按钮将在这个非常具体的布局下崩溃应用程序:

import SwiftUI

struct ContentView: View {
    @State var showControls: Bool = true
    
    var body: some View {
        ZStack {
            Rectangle().foregroundColor(Color.gray) // Removing this would fix it
            VStack {
                Text("someText")
                Text("anotherText") // Removing this would also fix it
            }
            if showControls {
                Text("Button")
                .zIndex(1) // Removing this would also fix it
            }
        }
        .onTapGesture {
            withAnimation(.easeInOut(duration: 1)) {  // Removing this would also fix it
                showControls.toggle()
            }
        }
    }
}

如代码注释中所述,这些更改中的任何一个都可以防止崩溃:

  1. 从 ZStack 底部移除 Rectangle
  2. 从 VStack 中删除两个视图之一
  3. 移除 zIndex (这将打破淡出动画)
  4. 移除显式动画调用

这是我的控制台输出: XCode 调试输出

我想知道这里发生了什么,是什么导致了崩溃?

标签: iosswiftui

解决方案


推荐阅读