70 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Swift
		
	
	
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Swift
		
	
	
	
	
	
import SwiftUI
 | 
						||
import UIKit
 | 
						||
 | 
						||
struct RefreshableScrollView<Content: View>: UIViewRepresentable {
 | 
						||
    var content: Content
 | 
						||
    var onRefresh: () -> Void
 | 
						||
    var isRefreshing: Binding<Bool>
 | 
						||
 | 
						||
    init(isRefreshing: Binding<Bool>, onRefresh: @escaping () -> Void, @ViewBuilder content: () -> Content) {
 | 
						||
        self.content = content()
 | 
						||
        self.onRefresh = onRefresh
 | 
						||
        self.isRefreshing = isRefreshing
 | 
						||
    }
 | 
						||
 | 
						||
    func makeUIView(context: Context) -> UIScrollView {
 | 
						||
        let scrollView = UIScrollView()
 | 
						||
        scrollView.delaysContentTouches = false
 | 
						||
        
 | 
						||
        let refreshControl = UIRefreshControl()
 | 
						||
        refreshControl.addTarget(context.coordinator, action: #selector(Coordinator.handleRefresh), for: .valueChanged)
 | 
						||
        scrollView.refreshControl = refreshControl
 | 
						||
 | 
						||
        let hostingController = UIHostingController(rootView: content)
 | 
						||
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
 | 
						||
        scrollView.addSubview(hostingController.view)
 | 
						||
 | 
						||
        NSLayoutConstraint.activate([
 | 
						||
            hostingController.view.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
 | 
						||
            hostingController.view.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
 | 
						||
            hostingController.view.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
 | 
						||
            hostingController.view.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
 | 
						||
            hostingController.view.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor)
 | 
						||
        ])
 | 
						||
        
 | 
						||
        context.coordinator.hostingController = hostingController
 | 
						||
        
 | 
						||
        return scrollView
 | 
						||
    }
 | 
						||
 | 
						||
    func updateUIView(_ uiView: UIScrollView, context: Context) {
 | 
						||
        if isRefreshing.wrappedValue {
 | 
						||
            uiView.refreshControl?.beginRefreshing()
 | 
						||
        } else {
 | 
						||
            // Отложенное завершение, чтобы избежать цикла обновлений
 | 
						||
            DispatchQueue.main.async {
 | 
						||
                uiView.refreshControl?.endRefreshing()
 | 
						||
            }
 | 
						||
        }
 | 
						||
        
 | 
						||
        context.coordinator.hostingController?.rootView = content
 | 
						||
    }
 | 
						||
 | 
						||
    func makeCoordinator() -> Coordinator {
 | 
						||
        Coordinator(self)
 | 
						||
    }
 | 
						||
 | 
						||
    class Coordinator: NSObject {
 | 
						||
        var parent: RefreshableScrollView
 | 
						||
        var hostingController: UIHostingController<Content>?
 | 
						||
 | 
						||
        init(_ parent: RefreshableScrollView) {
 | 
						||
            self.parent = parent
 | 
						||
        }
 | 
						||
 | 
						||
        @objc func handleRefresh() {
 | 
						||
            parent.onRefresh()
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |