summaryrefslogtreecommitdiff
path: root/Sora/Views/NativeInteractiveImageScrollView.swift
blob: 3ef26d3b288330aca6e37a5e78f2b9a89cde83a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#if os(iOS)
  import SwiftUI
  import UIKit

  struct NativeInteractiveImageScrollView<Content: View>: UIViewRepresentable {
    @Binding var isZoomed: Bool
    let contentIdentifier: String
    let minimumZoomScale: CGFloat
    let maximumZoomScale: CGFloat
    let doubleTapZoomScale: CGFloat
    let content: Content

    init(
      isZoomed: Binding<Bool>,
      contentIdentifier: String,
      minimumZoomScale: CGFloat,
      maximumZoomScale: CGFloat,
      doubleTapZoomScale: CGFloat,
      @ViewBuilder content: () -> Content
    ) {
      _isZoomed = isZoomed
      self.contentIdentifier = contentIdentifier
      self.minimumZoomScale = minimumZoomScale
      self.maximumZoomScale = maximumZoomScale
      self.doubleTapZoomScale = doubleTapZoomScale
      self.content = content()
    }

    func makeUIView(context: Context) -> UIScrollView {
      let scrollView = UIScrollView()

      scrollView.delegate = context.coordinator
      scrollView.minimumZoomScale = minimumZoomScale
      scrollView.maximumZoomScale = maximumZoomScale
      scrollView.zoomScale = minimumZoomScale
      scrollView.bouncesZoom = true
      scrollView.showsHorizontalScrollIndicator = false
      scrollView.showsVerticalScrollIndicator = false
      scrollView.contentInsetAdjustmentBehavior = .never
      scrollView.decelerationRate = .fast

      let hostingController = UIHostingController(rootView: content)

      hostingController.view.backgroundColor = .clear
      hostingController.view.translatesAutoresizingMaskIntoConstraints = false

      scrollView.addSubview(hostingController.view)

      NSLayoutConstraint.activate([
        hostingController.view.leadingAnchor.constraint(
          equalTo: scrollView.contentLayoutGuide.leadingAnchor
        ),
        hostingController.view.trailingAnchor.constraint(
          equalTo: scrollView.contentLayoutGuide.trailingAnchor
        ),
        hostingController.view.topAnchor.constraint(
          equalTo: scrollView.contentLayoutGuide.topAnchor
        ),
        hostingController.view.bottomAnchor.constraint(
          equalTo: scrollView.contentLayoutGuide.bottomAnchor
        ),
        hostingController.view.widthAnchor.constraint(
          equalTo: scrollView.frameLayoutGuide.widthAnchor
        ),
        hostingController.view.heightAnchor.constraint(
          equalTo: scrollView.frameLayoutGuide.heightAnchor
        ),
      ])

      let doubleTapGestureRecognizer = UITapGestureRecognizer(
        target: context.coordinator,
        action: #selector(Coordinator.handleDoubleTapGesture(_:))
      )

      doubleTapGestureRecognizer.numberOfTapsRequired = 2

      scrollView.addGestureRecognizer(doubleTapGestureRecognizer)

      context.coordinator.hostingController = hostingController
      context.coordinator.hostedView = hostingController.view
      context.coordinator.latestContentIdentifier = contentIdentifier

      context.coordinator.updateZoomState(for: scrollView)
      context.coordinator.recenterContent(in: scrollView)

      return scrollView
    }

    func updateUIView(_ scrollView: UIScrollView, context: Context) {
      context.coordinator.parent = self
      context.coordinator.hostingController?.rootView = content

      if context.coordinator.latestContentIdentifier != contentIdentifier {
        context.coordinator.latestContentIdentifier = contentIdentifier
        scrollView.setZoomScale(minimumZoomScale, animated: false)
        scrollView.contentOffset = .zero
      }

      context.coordinator.recenterContent(in: scrollView)
      context.coordinator.updateZoomState(for: scrollView)
    }

    func makeCoordinator() -> Coordinator {
      Coordinator(parent: self)
    }

    final class Coordinator: NSObject, UIScrollViewDelegate {
      var parent: NativeInteractiveImageScrollView
      var hostingController: UIHostingController<Content>?
      weak var hostedView: UIView?
      var latestContentIdentifier: String?

      init(parent: NativeInteractiveImageScrollView) {
        self.parent = parent
      }

      func viewForZooming(in _: UIScrollView) -> UIView? {
        hostedView
      }

      func scrollViewDidZoom(_ scrollView: UIScrollView) {
        recenterContent(in: scrollView)
        updateZoomState(for: scrollView)
      }

      @objc
      func handleDoubleTapGesture(_ recognizer: UITapGestureRecognizer) {
        guard let scrollView = recognizer.view as? UIScrollView else { return }

        let minimumZoomScale = scrollView.minimumZoomScale
        let maximumZoomScale = scrollView.maximumZoomScale
        let targetZoomScale = min(parent.doubleTapZoomScale, maximumZoomScale)

        if scrollView.zoomScale > minimumZoomScale + 0.01 {
          scrollView.setZoomScale(minimumZoomScale, animated: true)
          return
        }

        guard let hostedView else { return }

        let tapLocation = recognizer.location(in: hostedView)
        let zoomRectangle = zoomRectangle(
          for: targetZoomScale,
          centeredAt: tapLocation,
          in: scrollView
        )

        scrollView.zoom(to: zoomRectangle, animated: true)
      }

      func recenterContent(in scrollView: UIScrollView) {
        let horizontalInset = max((scrollView.bounds.width - scrollView.contentSize.width) / 2, 0)
        let verticalInset = max((scrollView.bounds.height - scrollView.contentSize.height) / 2, 0)

        scrollView.contentInset = UIEdgeInsets(
          top: verticalInset,
          left: horizontalInset,
          bottom: verticalInset,
          right: horizontalInset
        )
      }

      func updateZoomState(for scrollView: UIScrollView) {
        let isCurrentlyZoomed = scrollView.zoomScale > scrollView.minimumZoomScale + 0.01

        if parent.isZoomed != isCurrentlyZoomed {
          DispatchQueue.main.async {
            self.parent.isZoomed = isCurrentlyZoomed
          }
        }
      }

      private func zoomRectangle(
        for targetZoomScale: CGFloat,
        centeredAt tapLocation: CGPoint,
        in scrollView: UIScrollView
      ) -> CGRect {
        let rectangleWidth = scrollView.bounds.width / targetZoomScale
        let rectangleHeight = scrollView.bounds.height / targetZoomScale
        let horizontalOrigin = tapLocation.x - (rectangleWidth / 2)
        let verticalOrigin = tapLocation.y - (rectangleHeight / 2)

        return CGRect(
          x: horizontalOrigin,
          y: verticalOrigin,
          width: rectangleWidth,
          height: rectangleHeight
        )
      }
    }
  }
#endif