我正在开发一个新的SwiftUI应用,并且试图弄清楚如何使该Swift项目与SwiftUI兼容: https://github.com/suzuki-0000/SKPhotoBrowser
问题是我无法使UIViewRepresentable工作。 我收到错误消息:
类型'PhotoViewer'不符合协议'UIViewRepresentable'
这是我的代码:
struct PhotoViewer: UIViewRepresentable {
@Binding var viewerImages:[SKPhoto]
@Binding var currentPageIndex: Int
func makeUIView(context: Context) -> SKPhotoBrowser {
let browser = SKPhotoBrowser(photos: viewerImages)
browser.initializePageIndex(currentPageIndex)
browser.delegate = context.coordinator
return browser
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIView(_ browser: SKPhotoBrowser, context: Context) {
browser.photos = viewerImages
browser.currentPageIndex = currentPageIndex
}
class Coordinator: NSObject, SKPhotoBrowserDelegate {
var control: PhotoViewer
init(_ control: PhotoViewer) {
self.control = control
}
func didShowPhotoAtIndex(_ browser: PhotoViewer) {
self.control.currentPageIndex = browser.currentPageIndex
}
}
}
我在这里想念什么?
答案 0 :(得分:3)
SKPhotoBrowser
是UIViewController
的子类,因此您需要使其符合UIViewControllerRepresentable
而不是UIViewRepresentable
实际上,差异不大
struct PhotoViewer: UIViewControllerRepresentable {
@Binding var viewerImages:[SKPhoto]
@Binding var currentPageIndex: Int
func makeUIViewController(context: Context) -> SKPhotoBrowser {
let browser = SKPhotoBrowser(photos: viewerImages)
browser.initializePageIndex(currentPageIndex)
browser.delegate = context.coordinator
return browser
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIViewController(_ browser: SKPhotoBrowser, context: Context) {
browser.photos = viewerImages
browser.currentPageIndex = currentPageIndex
}
class Coordinator: NSObject, SKPhotoBrowserDelegate {
var control: PhotoViewer
init(_ control: PhotoViewer) {
self.control = control
}
func didShowPhotoAtIndex(_ browser: PhotoViewer) {
self.control.currentPageIndex = browser.currentPageIndex
}
}
}
答案 1 :(得分:0)
也许您忘记为UIViewType
明确指定类型别名了?