gpt4 book ai didi

swift - 如何触发 UIViewRepresentable 的 updateUIView?

转载 作者:行者123 更新时间:2023-12-04 03:44:02 29 4
gpt4 key购买 nike

我正在尝试从 SpritzSwift 实现 uiView进入 SwiftUI 应用程序,但在第一次渲染后我无法更新它。驱动 UIView 的管理器正在工作,但 UIView 本身没有更新。我希望 View Controller 内的 UiView.setNeedsDisplay() 或包装器内的 @Bindable 变量的更改会触发 updateUIView,但不会触发骰子。
无论我对 UiView 或其包装器进行什么更改,它都不会更新(例如,在代码示例中,背景永远不会更新为清除颜色)。如何让这个 View 更新?
这是 SwiftUI 代码:

import SwiftUI
struct Content: View {

@State public var ssManager:SpritzSwiftManager = SpritzSwiftManager(withText: "", andWordPerMinute: Int(200))
@State public var ssView:SpritzSwiftView = SpritzSwiftView(frame: CGRect(x: 0, y: 0, width: 200, height: 600 ))
@State private var currentWord = ""

var body: some View {
VStack {
Text("SpritzTest")
.padding()
let spritzUIView = SpritzUIViewRepresentable(SpritzView: $ssView,SpritzViewManager:$ssManager, CurrentWord: $currentWord)
spritzUIView.padding()
Button(action:
{
ssManager = SpritzSwiftManager(withText: "Text try one two three", andWordPerMinute: 200)
spritzUIView.SpritzView = SpritzSwiftView(frame: CGRect(x: 0, y: 0, width: 200, height: 40 ))
spritzUIView.SpritzView.backgroundColor = .clear
ssManager.startReading { (word, finished) in
if !finished {
self.ssView.updateWord(word!)
currentWord = word!.word
spritzUIView.CurrentWord = currentWord

}
}
})
{
Text("Start")
}
}
}
}
这是包装:
struct SpritzUIViewRepresentable : UIViewRepresentable{
@Binding var SpritzView:SpritzSwiftView
@Binding var SpritzViewManager:SpritzSwiftManager
@Binding var CurrentWord:String

func makeUIView(context: Context) -> SpritzSwiftView {
return SpritzView
}
func updateUIView(_ uiView: SpritzSwiftView, context: Context) {
}
}

最佳答案

您需要在 makeUIView 中创建 UIKit View 并通过 Binding 仅传递依赖数据。当相关状态 - 事实来源 - 改变时,该绑定(bind)更改调用 updateUIView ,你应该在哪里更新你的 UIKit View 。
这里只是简化的演示草图,以显示概念(可能有错别字):

struct SpritzUIViewRepresentable : UIViewRepresentable{
@Binding var currentWord: SpritzSwiftWord
@Binding var backgroundColor: UIColor

func makeUIView(context: Context) -> SpritzSwiftView {
// create and configure view here
return SpritzSwiftView(frame: CGRect.zero) // frame does not matter here
}

func updateUIView(_ uiView: SpritzSwiftView, context: Context) {
// update view properties here from bound external data
uiView.backgroundColor = backgroundColor
uiView.updateWord(currentWord)
}
}
和按钮现在应该只更改模型数据
    VStack {
Text("SpritzTest")
.padding()
SpritzUIViewRepresentable(backgroundColor: $backgroundColor, SpritzViewManager:$ssManager, currentWord: $currentWord)
.padding()
Button(action:
{
ssManager = SpritzSwiftManager(withText: "Text try one two three", andWordPerMinute: 200)

self.backgroundColor = .clear
ssManager.startReading { (word, finished) in
if !finished {
self.currentWord = word
}
}
})
{
Text("Start")
}
假设更新的属性
@State private var currentWord = SpritzSwiftWord(word: "")
@State private var backgroundColor = UIColor.white // whatever you want

关于swift - 如何触发 UIViewRepresentable 的 updateUIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65461516/

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