gpt4 book ai didi

ios - 取消向后滑动时,SwiftUI 应用程序卡住,100% CPU

转载 作者:行者123 更新时间:2023-12-01 15:49:04 25 4
gpt4 key购买 nike

以下代码可能会使正在运行的应用程序在 iPhone(而非 iPad)或 iPhone 模拟器上无响应。 Xcode 显示该应用程序消耗 100% CPU,同时分配越来越多的内存。

struct SecondView: View {
@State private var keyboardHeight: CGFloat = 0

private let showPublisher = NotificationCenter.Publisher.init(
center: .default,
name: UIResponder.keyboardWillShowNotification
).map { (notification) -> CGFloat in
if let rect = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect {
return rect.size.height
} else {
return 0
}
}

var body: some View {
VStack(spacing: 20) {
if keyboardHeight == 0 {
Text("This is shown as long as there's no keyboard")
}
Text("This is the SecondView. Drag from the left edge to navigate back, but don't complete the gesture: crash results.")
}.onReceive(self.showPublisher) { (height) in
self.keyboardHeight = height
}
.navigationBarItems(trailing: Button("Dummy") { })
}
}

struct ContentView: View {
@State var textInput = ""

var body: some View {
NavigationView {
VStack(spacing: 20) {
TextField("1. Tap here to show keyboard", text: self.$textInput)
.textFieldStyle(RoundedBorderTextFieldStyle())
NavigationLink(destination: SecondView()) {
Text("2. Go to second screen")
}
Spacer()
}
}
}
}

触发卡住:
  • 点击文本框使键盘出现
  • 点击链接转到下一个屏幕
  • 从屏幕左侧拖动,但不要完成手势,而是提前释放

  • 有一些解决方法:
  • 删除 SecondView 中的导航栏项目(虚拟按钮)
  • 删除使用keyboardHeight SecondView 中的变量
  • 不要在ContentView中激活键盘导航前

  • 但是,我无法在我的应用程序中使用上述解决方法。有谁知道根本原因是什么?

    最佳答案

    我能够在导航之前停用键盘,使用以下解决方法:

    NavigationLink(destination: SecondView(), tag: 2, selection: $navigationSelection) {
    EmptyView()
    }
    Text("2. Go to second screen")
    .onTapGesture {
    UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    self.navigationSelection = 2
    }

    关于ios - 取消向后滑动时,SwiftUI 应用程序卡住,100% CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61250748/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com