斯威夫特 5.0 iOS 13
试图理解 UIViewRepresentable 是如何工作的,并把这个简单的例子放在一起,几乎就在那里,但也许它完全是胡说八道。是的,我知道 SwiftUI 中已经有一个 tapGesture,这只是一个测试。
不会编译,因为它说在从初始化程序返回之前没有在所有路径上调用“super.init”,我尝试设置但显然不正确。
import SwiftUI
struct newView: UIViewRepresentable {
typealias UIViewType = UIView
var v = UIView()
func updateUIView(_ uiView: UIView, context: Context) {
v.backgroundColor = UIColor.yellow
}
func makeUIView(context: Context) -> UIView {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(Coordinator.handleTap(sender:)))
v.addGestureRecognizer(tapGesture)
return v
}
func makeCoordinator() -> newView.Coordinator {
Coordinator(v)
}
final class Coordinator: UIView {
private let view: UIView
init(_ view: UIView) {
self.view = view
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func handleTap(sender: UITapGestureRecognizer) {
print("tap")
}
}
}