Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new scroll view extension to read content offset
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import SwiftUI | ||
import UIKit | ||
|
||
/// Hosting controller that updates the scroll view offset (x,y) | ||
class ScrollViewOffSetReader<Content>: UIHostingController<Content> where Content: View { | ||
|
||
var offset: Binding<CGFloat> | ||
let isOffsetX: Bool | ||
var showed = false | ||
var sv: UIScrollView? | ||
|
||
init(offset: Binding<CGFloat>, isOffsetX: Bool, rootView: Content) { | ||
self.offset = offset | ||
self.isOffsetX = isOffsetX | ||
super.init(rootView: rootView) | ||
} | ||
|
||
@objc dynamic required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidAppear(_ animated: Bool) { | ||
if showed { | ||
return | ||
} | ||
showed = true | ||
|
||
sv = findScrollView(in: view) | ||
|
||
sv?.addObserver(self, | ||
forKeyPath: #keyPath(UIScrollView.contentOffset), | ||
options: [.old, .new], | ||
context: nil) | ||
|
||
scroll(to: offset.wrappedValue, animated: false) | ||
|
||
super.viewDidAppear(animated) | ||
} | ||
|
||
func scroll(to position: CGFloat, animated: Bool = true) { | ||
if let s = sv { | ||
if position != (self.isOffsetX ? s.contentOffset.x : s.contentOffset.y) { | ||
let offset = self.isOffsetX ? CGPoint(x: position, y: 0) : CGPoint(x: 0, y: position) | ||
sv?.setContentOffset(offset, animated: animated) | ||
} | ||
} | ||
} | ||
|
||
override func observeValue(forKeyPath keyPath: String?, | ||
of object: Any?, | ||
change: [NSKeyValueChangeKey: Any]?, | ||
context: UnsafeMutableRawPointer?) { | ||
if keyPath == #keyPath(UIScrollView.contentOffset) { | ||
if let s = self.sv { | ||
DispatchQueue.main.async { | ||
self.offset.wrappedValue = self.isOffsetX ? s.contentOffset.x : s.contentOffset.y | ||
} | ||
} | ||
} | ||
} | ||
|
||
func findScrollView(in view: UIView?) -> UIScrollView? { | ||
if view?.isKind(of: UIScrollView.self) ?? false { | ||
return view as? UIScrollView | ||
} | ||
|
||
for subview in view?.subviews ?? [] { | ||
if let sv = findScrollView(in: subview) { | ||
return sv | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
struct ScrollViewOffSetReaderRepresentable<Content>: UIViewControllerRepresentable where Content: View { | ||
|
||
var offset: Binding<CGFloat> | ||
let isOffsetX: Bool | ||
let content: Content | ||
|
||
init(offset: Binding<CGFloat>, isOffsetX: Bool, @ViewBuilder content: @escaping () -> Content) { | ||
|
||
self.offset = offset | ||
self.isOffsetX = isOffsetX | ||
self.content = content() | ||
} | ||
|
||
func makeUIViewController(context: Context) -> ScrollViewOffSetReader<Content> { | ||
ScrollViewOffSetReader(offset: offset, isOffsetX: isOffsetX, rootView: content) | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: ScrollViewOffSetReader<Content>, context: Context) { | ||
uiViewController.scroll(to: offset.wrappedValue, animated: true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters