@@ -2,15 +2,15 @@ import SwiftUI
2
2
import UIKit
3
3
import URLImage
4
4
import Combine
5
+ import ImageViewer
5
6
6
7
@available ( iOS 13 . 0 , * )
7
8
public struct ImageViewerRemote : View {
8
9
@Binding var viewerShown : Bool
9
- @Binding var imageURL : String
10
- @State var httpHeaders : [ String : String ] ?
11
- @State var disableCache : Bool ?
12
- @State var caption : Text ?
13
- @State var closeButtonAlignment : CloseButtonAlignment ? = CloseButtonAlignment . topLeft
10
+ var imageURL : String
11
+ var disableCache : Bool ?
12
+ var caption : Text ?
13
+ var closeButtonAlignment : CloseButtonAlignment ? = CloseButtonAlignment . topLeft
14
14
15
15
var aspectRatio : Binding < CGFloat > ?
16
16
@@ -19,15 +19,15 @@ public struct ImageViewerRemote: View {
19
19
20
20
@ObservedObject var loader : ImageLoader
21
21
22
- public init ( imageURL: Binding < String > , viewerShown: Binding < Bool > , aspectRatio: Binding < CGFloat > ? = nil , disableCache: Bool ? = nil , caption: Text ? = nil , closeButtonAlignment: CloseButtonAlignment ? ) {
23
- _imageURL = imageURL
22
+ public init ( imageURL: String , viewerShown: Binding < Bool > , aspectRatio: Binding < CGFloat > ? = nil , disableCache: Bool ? = nil , caption: Text ? = nil , closeButtonAlignment: CloseButtonAlignment ? ) {
23
+ self . imageURL = imageURL
24
24
_viewerShown = viewerShown
25
- _disableCache = State ( initialValue : disableCache)
25
+ self . disableCache = disableCache
26
26
self . aspectRatio = aspectRatio
27
- _caption = State ( initialValue : caption)
28
- _closeButtonAlignment = State ( initialValue : closeButtonAlignment)
27
+ self . caption = caption
28
+ self . closeButtonAlignment = closeButtonAlignment
29
29
30
- loader = ImageLoader ( url: imageURL)
30
+ loader = ImageLoader ( url: . constant ( imageURL) )
31
31
}
32
32
33
33
@ViewBuilder
@@ -171,164 +171,6 @@ public struct ImageViewerRemote: View {
171
171
}
172
172
}
173
173
174
- class PinchZoomView : UIView {
175
-
176
- weak var delegate : PinchZoomViewDelgate ?
177
-
178
- private( set) var scale : CGFloat = 0 {
179
- didSet {
180
- delegate? . pinchZoomView ( self , didChangeScale: scale)
181
- }
182
- }
183
-
184
- private( set) var anchor : UnitPoint = . center {
185
- didSet {
186
- delegate? . pinchZoomView ( self , didChangeAnchor: anchor)
187
- }
188
- }
189
-
190
- private( set) var offset : CGSize = . zero {
191
- didSet {
192
- delegate? . pinchZoomView ( self , didChangeOffset: offset)
193
- }
194
- }
195
-
196
- private( set) var isPinching : Bool = false {
197
- didSet {
198
- delegate? . pinchZoomView ( self , didChangePinching: isPinching)
199
- }
200
- }
201
-
202
- private var startLocation : CGPoint = . zero
203
- private var location : CGPoint = . zero
204
- private var numberOfTouches : Int = 0
205
-
206
- init ( ) {
207
- super. init ( frame: . zero)
208
-
209
- let pinchGesture = UIPinchGestureRecognizer ( target: self , action: #selector( pinch ( gesture: ) ) )
210
- pinchGesture. cancelsTouchesInView = false
211
- addGestureRecognizer ( pinchGesture)
212
- }
213
-
214
- required init ? ( coder: NSCoder ) {
215
- fatalError ( )
216
- }
217
-
218
- @objc private func pinch( gesture: UIPinchGestureRecognizer ) {
219
-
220
- switch gesture. state {
221
- case . began:
222
- isPinching = true
223
- startLocation = gesture. location ( in: self )
224
- anchor = UnitPoint ( x: startLocation. x / bounds. width, y: startLocation. y / bounds. height)
225
- numberOfTouches = gesture. numberOfTouches
226
-
227
- case . changed:
228
- if gesture. numberOfTouches != numberOfTouches {
229
- // If the number of fingers being used changes, the start location needs to be adjusted to avoid jumping.
230
- let newLocation = gesture. location ( in: self )
231
- let jumpDifference = CGSize ( width: newLocation. x - location. x, height: newLocation. y - location. y)
232
- startLocation = CGPoint ( x: startLocation. x + jumpDifference. width, y: startLocation. y + jumpDifference. height)
233
-
234
- numberOfTouches = gesture. numberOfTouches
235
- }
236
-
237
- scale = gesture. scale
238
-
239
- location = gesture. location ( in: self )
240
- offset = CGSize ( width: location. x - startLocation. x, height: location. y - startLocation. y)
241
-
242
- case . ended, . cancelled, . failed:
243
- withAnimation ( . interactiveSpring( ) ) {
244
- isPinching = false
245
- scale = 1.0
246
- anchor = . center
247
- offset = . zero
248
- }
249
- default :
250
- break
251
- }
252
- }
253
-
254
- }
255
-
256
- protocol PinchZoomViewDelgate : AnyObject {
257
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangePinching isPinching: Bool )
258
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangeScale scale: CGFloat )
259
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangeAnchor anchor: UnitPoint )
260
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangeOffset offset: CGSize )
261
- }
262
-
263
- struct PinchZoom : UIViewRepresentable {
264
-
265
- @Binding var scale : CGFloat
266
- @Binding var anchor : UnitPoint
267
- @Binding var offset : CGSize
268
- @Binding var isPinching : Bool
269
-
270
- func makeCoordinator( ) -> Coordinator {
271
- Coordinator ( self )
272
- }
273
-
274
- func makeUIView( context: Context ) -> PinchZoomView {
275
- let pinchZoomView = PinchZoomView ( )
276
- pinchZoomView. delegate = context. coordinator
277
- return pinchZoomView
278
- }
279
-
280
- func updateUIView( _ pageControl: PinchZoomView , context: Context ) { }
281
-
282
- class Coordinator : NSObject , PinchZoomViewDelgate {
283
- var pinchZoom : PinchZoom
284
-
285
- init ( _ pinchZoom: PinchZoom ) {
286
- self . pinchZoom = pinchZoom
287
- }
288
-
289
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangePinching isPinching: Bool ) {
290
- pinchZoom. isPinching = isPinching
291
- }
292
-
293
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangeScale scale: CGFloat ) {
294
- pinchZoom. scale = scale
295
- }
296
-
297
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangeAnchor anchor: UnitPoint ) {
298
- pinchZoom. anchor = anchor
299
- }
300
-
301
- func pinchZoomView( _ pinchZoomView: PinchZoomView , didChangeOffset offset: CGSize ) {
302
- pinchZoom. offset = offset
303
- }
304
- }
305
- }
306
-
307
- struct PinchToZoom : ViewModifier {
308
- @State var scale : CGFloat = 1.0
309
- @State var anchor : UnitPoint = . center
310
- @State var offset : CGSize = . zero
311
- @State var isPinching : Bool = false
312
-
313
- func body( content: Content ) -> some View {
314
- content
315
- . scaleEffect ( scale, anchor: anchor)
316
- . offset ( offset)
317
- . overlay ( PinchZoom ( scale: $scale, anchor: $anchor, offset: $offset, isPinching: $isPinching) )
318
- }
319
- }
320
-
321
- extension View {
322
- func pinchToZoom( ) -> some View {
323
- self . modifier ( PinchToZoom ( ) )
324
- }
325
- }
326
-
327
-
328
-
329
-
330
-
331
-
332
174
class ImageLoader : ObservableObject {
333
175
@Published var image : UIImage ?
334
176
private let url : Binding < String >
@@ -366,12 +208,3 @@ class ImageLoader: ObservableObject {
366
208
cancellable? . cancel ( )
367
209
}
368
210
}
369
-
370
-
371
- public enum CloseButtonAlignment {
372
- case topLeft
373
- case topRight
374
- case bottomLeft
375
- case bottomRight
376
- case hidden
377
- }
0 commit comments