Last active
March 18, 2025 12:26
-
-
Save Chronos2500/1e28d254f6e5a77c760180a96067b65d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import SwiftUI | |
| struct ContentView: View { | |
| @State var maxNumber: Int = 50 | |
| var body: some View { | |
| Text(verbatim: "Bounce-BasedOnSize") | |
| Slider(value: Binding( | |
| get: { Double(maxNumber) }, | |
| set: { maxNumber = Int($0) } | |
| ), in: 1...50, step: 1) | |
| .padding() | |
| Divider() | |
| ScrollView{ | |
| VStack { | |
| ForEach(1...maxNumber, id: \.self) { number in | |
| Text("\(number)") | |
| } | |
| } | |
| .id(String(maxNumber)) | |
| .frame(maxWidth: .infinity) | |
| } | |
| .viewExtractor{ view in | |
| guard let scrollView = view as? UIScrollView else { return } | |
| scrollView.alwaysBounceVertical = false | |
| } | |
| } | |
| } | |
| extension View { | |
| @ViewBuilder | |
| func viewExtractor(result: @escaping (UIView) -> ()) -> some View { | |
| self | |
| .background(UIKitHelper(result: result)) | |
| .compositingGroup() | |
| } | |
| } | |
| fileprivate struct UIKitHelper: UIViewRepresentable { | |
| var result: (UIView) -> () | |
| func makeUIView(context: Context) -> UIView { | |
| let view = UIView(frame: .zero) | |
| view.backgroundColor = .clear | |
| view.isUserInteractionEnabled = false | |
| DispatchQueue.main.async { | |
| if let uiKitView = view.superview?.superview?.subviews.last?.subviews.first { | |
| result(uiKitView) | |
| } | |
| } | |
| return view | |
| } | |
| func updateUIView(_ uiView: UIView, context: Context) {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment