-
Notifications
You must be signed in to change notification settings - Fork 389
-
After I update the library to last version
this parts that I don't now how to update
private func tableView(entry: UIView) -> UITableView? {
// Search in ancestors
if let tableView = Introspect.findAncestor(ofType: UITableView.self, from: entry) {
return tableView
}
guard let viewHost = Introspect.findViewHost(from: entry) else {
return nil
}
// Search in siblings
return Introspect.previousSibling(containing: UITableView.self, from: viewHost)
}
//and
private func collectionView(entry: UIView) -> UICollectionView? {
// Search in ancestors
if let collectionView = Introspect.findAncestor(ofType: UICollectionView.self, from: entry) {
return collectionView
}
guard let viewHost = Introspect.findViewHost(from: entry) else {
return nil
}
// Search in siblings
return Introspect.previousSibling(containing: UICollectionView.self, from: viewHost)
}
```
-------------
**Full Code**
private struct PullToRefresh: UIViewRepresentable {
let onRefresh: () -> Void
public init(
onRefresh: @escaping () -> Void
) {
self.onRefresh = onRefresh
}
public class Coordinator {
let onRefresh: () -> Void
var isShowing: Bool = false
var tableview: UITableView?
init(
onRefresh: @escaping () -> Void
) {
self.onRefresh = onRefresh
}
@objc
func onValueChanged() {
guard isShowing == false else {return}
isShowing = false
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [self] in
tableview?.refreshControl?.endRefreshing()
onRefresh()
}
}
}
public func makeUIView(context: UIViewRepresentableContext<PullToRefresh>) -> UIView {
let view = UIView(frame: .zero)
view.isHidden = true
view.isUserInteractionEnabled = false
return view
}
private func tableView(entry: UIView) -> UITableView? {
// Search in ancestors
if let tableView = Introspect.findAncestor(ofType: UITableView.self, from: entry) {
return tableView
}
guard let viewHost = Introspect.findViewHost(from: entry) else {
return nil
}
// Search in siblings
return Introspect.previousSibling(containing: UITableView.self, from: viewHost)
}
public func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PullToRefresh>) {
DispatchQueue.main.asyncAfter(deadline: .now()) {
guard let tableView = self.tableView(entry: uiView) else {
return
}
context.coordinator.tableview = tableView
context.coordinator.tableview?.refreshControl = UIRefreshControl()
context.coordinator.tableview?.refreshControl?.addTarget(context.coordinator, action: #selector(context.coordinator.onValueChanged), for: .valueChanged)
}
}
public func makeCoordinator() -> Coordinator {
return Coordinator(onRefresh: onRefresh)
}
}
private struct PullToRefreshIOS16: UIViewRepresentable {
let onRefresh: () -> Void
public init(
onRefresh: @escaping () -> Void
) {
self.onRefresh = onRefresh
}
public class Coordinator {
let onRefresh: () -> Void
var isShowing: Bool = false
var collectionView: UICollectionView?
init(
onRefresh: @escaping () -> Void
) {
self.onRefresh = onRefresh
}
@objc
func onValueChanged() {
guard isShowing == false else {return}
isShowing = false
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [self] in
collectionView?.refreshControl?.endRefreshing()
onRefresh()
}
}
}
public func makeUIView(context: UIViewRepresentableContext<PullToRefreshIOS16>) -> UIView {
let view = UIView(frame: .zero)
view.isHidden = true
view.isUserInteractionEnabled = false
return view
}
private func collectionView(entry: UIView) -> UICollectionView? {
// Search in ancestors
if let collectionView = Introspect.findAncestor(ofType: UICollectionView.self, from: entry) {
return collectionView
}
guard let viewHost = Introspect.findViewHost(from: entry) else {
return nil
}
// Search in siblings
return Introspect.previousSibling(containing: UICollectionView.self, from: viewHost)
}
public func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PullToRefreshIOS16>) {
DispatchQueue.main.asyncAfter(deadline: .now()) {
guard let collectionView = self.collectionView(entry: uiView) else {
return
}
context.coordinator.collectionView = collectionView
context.coordinator.collectionView?.refreshControl = UIRefreshControl()
context.coordinator.collectionView?.refreshControl?.addTarget(context.coordinator, action: #selector(context.coordinator.onValueChanged), for: .valueChanged)
}
}
public func makeCoordinator() -> Coordinator {
return Coordinator(onRefresh: onRefresh)
}
}
extension View {
public func PullToRefresh(onRefresh: @escaping () -> Void) -> some View {
return overlay {
if #available(iOS 16.0, *) {
PullToRefreshIOS16(onRefresh: onRefresh)
.frame(width: 0, height: 0)
} else {
PullToRefresh(onRefresh: onRefresh)
.frame(width: 0, height: 0)
}
}
}
}
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment