|
| 1 | +// |
| 2 | +// Copyright © 2021 Stream.io Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import Combine |
| 6 | +import SwiftUI |
| 7 | + |
| 8 | +struct ZoomableScrollView<Content: View>: View { |
| 9 | + let content: Content |
| 10 | + |
| 11 | + init(@ViewBuilder content: () -> Content) { |
| 12 | + self.content = content() |
| 13 | + } |
| 14 | + |
| 15 | + @State var doubleTap = PassthroughSubject<Void, Never>() |
| 16 | + |
| 17 | + var body: some View { |
| 18 | + ZoomableScrollViewImpl( |
| 19 | + content: content, |
| 20 | + doubleTap: doubleTap.eraseToAnyPublisher() |
| 21 | + ) |
| 22 | + .onTapGesture(count: 2) { |
| 23 | + doubleTap.send() |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +private struct ZoomableScrollViewImpl<Content: View>: UIViewControllerRepresentable { |
| 29 | + let content: Content |
| 30 | + let doubleTap: AnyPublisher<Void, Never> |
| 31 | + |
| 32 | + func makeUIViewController(context: Context) -> ZoomableScrollViewController { |
| 33 | + ZoomableScrollViewController(coordinator: context.coordinator, doubleTap: doubleTap) |
| 34 | + } |
| 35 | + |
| 36 | + func makeCoordinator() -> Coordinator { |
| 37 | + Coordinator(hostingController: UIHostingController(rootView: content)) |
| 38 | + } |
| 39 | + |
| 40 | + func updateUIViewController(_ viewController: ZoomableScrollViewController, context: Context) { |
| 41 | + viewController.update(content: content, doubleTap: doubleTap) |
| 42 | + } |
| 43 | + |
| 44 | + // MARK: - ZoomableScrollViewController |
| 45 | + |
| 46 | + class ZoomableScrollViewController: UIViewController, UIScrollViewDelegate { |
| 47 | + let coordinator: Coordinator |
| 48 | + let scrollView = UIScrollView() |
| 49 | + |
| 50 | + var doubleTapCancellable: Cancellable? |
| 51 | + var updateConstraintsCancellable: Cancellable? |
| 52 | + |
| 53 | + private var hostedView: UIView { coordinator.hostingController.view! } |
| 54 | + |
| 55 | + private var contentSizeConstraints: [NSLayoutConstraint] = [] { |
| 56 | + willSet { NSLayoutConstraint.deactivate(contentSizeConstraints) } |
| 57 | + didSet { NSLayoutConstraint.activate(contentSizeConstraints) } |
| 58 | + } |
| 59 | + |
| 60 | + @available(*, unavailable) |
| 61 | + required init?(coder: NSCoder) { fatalError() } |
| 62 | + init(coordinator: Coordinator, doubleTap: AnyPublisher<Void, Never>) { |
| 63 | + self.coordinator = coordinator |
| 64 | + super.init(nibName: nil, bundle: nil) |
| 65 | + view = scrollView |
| 66 | + |
| 67 | + scrollView.delegate = self |
| 68 | + scrollView.maximumZoomScale = 10 |
| 69 | + scrollView.minimumZoomScale = 1 |
| 70 | + scrollView.bouncesZoom = true |
| 71 | + scrollView.showsHorizontalScrollIndicator = false |
| 72 | + scrollView.showsVerticalScrollIndicator = false |
| 73 | + scrollView.clipsToBounds = false |
| 74 | + |
| 75 | + let hostedView = coordinator.hostingController.view! |
| 76 | + hostedView.translatesAutoresizingMaskIntoConstraints = false |
| 77 | + scrollView.addSubview(hostedView) |
| 78 | + NSLayoutConstraint.activate([ |
| 79 | + hostedView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor), |
| 80 | + hostedView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor), |
| 81 | + hostedView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor), |
| 82 | + hostedView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor) |
| 83 | + ]) |
| 84 | + |
| 85 | + updateConstraintsCancellable = scrollView.publisher(for: \.bounds).map(\.size).removeDuplicates() |
| 86 | + .sink { [unowned self] _ in |
| 87 | + view.setNeedsUpdateConstraints() |
| 88 | + } |
| 89 | + doubleTapCancellable = doubleTap.sink { [unowned self] in handleDoubleTap() } |
| 90 | + } |
| 91 | + |
| 92 | + func update(content: Content, doubleTap: AnyPublisher<Void, Never>) { |
| 93 | + coordinator.hostingController.rootView = content |
| 94 | + scrollView.setNeedsUpdateConstraints() |
| 95 | + doubleTapCancellable = doubleTap.sink { [unowned self] in handleDoubleTap() } |
| 96 | + } |
| 97 | + |
| 98 | + func handleDoubleTap() { |
| 99 | + scrollView.setZoomScale( |
| 100 | + scrollView.zoomScale > 1 ? scrollView.minimumZoomScale : 2, |
| 101 | + animated: true |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + override func updateViewConstraints() { |
| 106 | + super.updateViewConstraints() |
| 107 | + let hostedContentSize = coordinator.hostingController.sizeThatFits(in: view.bounds.size) |
| 108 | + contentSizeConstraints = [ |
| 109 | + hostedView.widthAnchor.constraint(equalToConstant: hostedContentSize.width), |
| 110 | + hostedView.heightAnchor.constraint(equalToConstant: hostedContentSize.height) |
| 111 | + ] |
| 112 | + } |
| 113 | + |
| 114 | + override func viewDidAppear(_ animated: Bool) { |
| 115 | + scrollView.zoom(to: hostedView.bounds, animated: false) |
| 116 | + } |
| 117 | + |
| 118 | + override func viewDidLayoutSubviews() { |
| 119 | + super.viewDidLayoutSubviews() |
| 120 | + |
| 121 | + let hostedContentSize = coordinator.hostingController.sizeThatFits(in: view.bounds.size) |
| 122 | + scrollView.minimumZoomScale = min( |
| 123 | + scrollView.bounds.width / hostedContentSize.width, |
| 124 | + scrollView.bounds.height / hostedContentSize.height |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { |
| 129 | + coordinator.animate(alongsideTransition: { _ in |
| 130 | + self.scrollView.zoom(to: self.hostedView.bounds, animated: false) |
| 131 | + }) |
| 132 | + } |
| 133 | + |
| 134 | + func viewForZooming(in scrollView: UIScrollView) -> UIView? { |
| 135 | + hostedView |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // MARK: - Coordinator |
| 140 | + |
| 141 | + class Coordinator: NSObject, UIScrollViewDelegate { |
| 142 | + var hostingController: UIHostingController<Content> |
| 143 | + |
| 144 | + init(hostingController: UIHostingController<Content>) { |
| 145 | + self.hostingController = hostingController |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments