[ad_1]
I created a UIScrollView for SwiftUI as follows:
ScrollView
import SwiftUI
struct ZoomableScrollView<Content material: View>: UIViewRepresentable {
personal var content material: Content material
init(@ViewBuilder content material: () -> Content material) {
self.content material = content material()
}
func makeUIView(context: Context) -> UIScrollView {
// arrange the UIScrollView
let scrollView = UIScrollView()
scrollView.delegate = context.coordinator // for viewForZooming(in:)
scrollView.maximumZoomScale = 4
scrollView.minimumZoomScale = 1
scrollView.bouncesZoom = false
// Indicators
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
// create a UIHostingController to carry our SwiftUI content material
let hostedView = context.coordinator.hostingController.view!
hostedView.translatesAutoresizingMaskIntoConstraints = true
hostedView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
hostedView.body = scrollView.bounds
scrollView.addSubview(hostedView)
return scrollView
}
func makeCoordinator() -> Coordinator {
return Coordinator(hostingController: UIHostingController(rootView: self.content material))
}
func updateUIView(_ uiView: UIScrollView, context: Context) {
// replace the internet hosting controller's SwiftUI content material
context.coordinator.hostingController.rootView = self.content material
assert(context.coordinator.hostingController.view.superview == uiView)
}
// MARK: - Coordinator
class Coordinator: NSObject, UIScrollViewDelegate {
var hostingController: UIHostingController<Content material>
init(hostingController: UIHostingController<Content material>) {
self.hostingController = hostingController
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return hostingController.view
}
}
}
ContentView
import SwiftUI
struct ContentView: View {
var physique: some View {
GeometryReader { geo in
ZoomableScrollView {
Rectangle()
.foregroundColor(.blue)
.body(width: 100, peak: 100)
}
.edgesIgnoringSafeArea(.all)
}
}
}
Now I would like the content material of the UIScrollView to bounce again to the sting when dragged off the display screen.
I’ve discovered a number of tutorials on how to do that with a UIImage, nonetheless I do not know find out how to apply this to my code.
[ad_2]
