我已使用UIScrollView
在SwiftUI中实现了自定义UIViewRepresentable
,我想检测列表的底部,以便加载下一批数据。我找到了一些答案,但是它们似乎在我的UIViewRepresentable
中不起作用。
我已经在协调器中实现了此功能,但是当我到达最低点时什么也没有发生。
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
print("bottom reached")
// not working
}
}
问题可能出在我的Swift视图中而不是UIViewRepresentable
中吗??
struct CustomScrollView<Content: View, VM: LoadProtocol> : UIViewRepresentable {
var width : CGFloat
var height : CGFloat
var viewModel: VM
let content: () -> Content
func makeCoordinator() -> Coordinator {
Coordinator(self, viewModel: viewModel)
}
func makeUIView(context: Context) -> UIScrollView {
let control = UIScrollView()
control.refreshControl = UIRefreshControl()
control.refreshControl?.addTarget(context.coordinator, action: #selector(Coordinator.handleRefreshControl), for: .valueChanged)
let childView = UIHostingController(rootView: content())
childView.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
control.addSubview(childView.view)
return control
}
func updateUIView(_ uiView: UIScrollView, context: Context) { }
class Coordinator: NSObject {
var control: CustomScrollView<Content, VM>
var viewModel: VM
init(_ control: CustomScrollView, viewModel: VM) {
self.control = control
self.viewModel = viewModel
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
print("bottom reached")
// not working
}
}
@objc func handleRefreshControl(sender: UIRefreshControl) {
sender.endRefreshing()
viewModel.refresh()
}
}
}
struct SeeAllView: View {
@ObservedObject var seeAllViewModel: SeeAllViewModel
var body: some View {
VStack {
GeometryReader { geometry in
CustomScrollView(width: geometry.size.width, height: geometry.size.height, viewModel: self.seeAllViewModel) {
SeeAllListView(seeAllViewModel: self.seeAllViewModel)
}
}
}
}
}
答案 0 :(得分:1)
您没有在makeUIView
中设置委托。以下更改将修复您的代码-我删除了一些缺少的依赖关系,无法使代码得以编译。它可以在Playground中正常运行。
import SwiftUI
import PlaygroundSupport
struct CustomScrollView<Content: View> : UIViewRepresentable {
var width : CGFloat
var height : CGFloat
let content: () -> Content
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UIScrollView {
let control = UIScrollView()
control.refreshControl = UIRefreshControl()
control.refreshControl?.addTarget(context.coordinator, action: #selector(Coordinator.handleRefreshControl), for: .valueChanged)
control.delegate = context.coordinator
let childView = UIHostingController(rootView: content())
childView.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
control.addSubview(childView.view)
return control
}
func updateUIView(_ uiView: UIScrollView, context: Context) { }
class Coordinator: NSObject, UIScrollViewDelegate {
var control: CustomScrollView<Content>
init(_ control: CustomScrollView) {
self.control = control
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y + 1) >= (scrollView.contentSize.height - scrollView.frame.size.height) {
print("bottom reached")
// not working
}
}
@objc func handleRefreshControl(sender: UIRefreshControl) {
sender.endRefreshing()
}
}
}
struct SeeAllView: View {
var body: some View {
VStack {
GeometryReader { geometry in
CustomScrollView(width: geometry.size.width, height: geometry.size.height) {
List {
Text("1")
Text("2")
Text("3")
}
}
}
}
}
}
PlaygroundPage.current.setLiveView(SeeAllView())