mirror of
https://github.com/yattee/yattee.git
synced 2024-12-23 22:13:41 +00:00
43 lines
1.3 KiB
Swift
43 lines
1.3 KiB
Swift
|
//
|
||
|
// RefreshControlModifier.swift
|
||
|
// SwiftUI_Pull_to_Refresh
|
||
|
//
|
||
|
// Created by Geri Borbás on 18/09/2021.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
import SwiftUI
|
||
|
|
||
|
struct RefreshControlModifier: ViewModifier {
|
||
|
@State private var geometryReaderFrame: CGRect = .zero
|
||
|
let refreshControl: RefreshControl
|
||
|
|
||
|
internal init(onValueChanged: @escaping (UIRefreshControl) -> Void) {
|
||
|
refreshControl = RefreshControl(onValueChanged: onValueChanged)
|
||
|
}
|
||
|
|
||
|
func body(content: Content) -> some View {
|
||
|
content
|
||
|
.background(
|
||
|
GeometryReader { geometry in
|
||
|
ScrollViewMatcher(
|
||
|
onResolve: { scrollView in
|
||
|
refreshControl.add(to: scrollView)
|
||
|
},
|
||
|
geometryReaderFrame: $geometryReaderFrame
|
||
|
)
|
||
|
.preference(key: FramePreferenceKey.self, value: geometry.frame(in: .global))
|
||
|
.onPreferenceChange(FramePreferenceKey.self) { frame in
|
||
|
self.geometryReaderFrame = frame
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension View {
|
||
|
func refreshControl(onValueChanged: @escaping (_ refreshControl: UIRefreshControl) -> Void) -> some View {
|
||
|
modifier(RefreshControlModifier(onValueChanged: onValueChanged))
|
||
|
}
|
||
|
}
|