@@ -2,15 +2,15 @@ import SwiftUI
22import UIKit
33import URLImage
44import Combine
5+ import ImageViewer
56
67@available ( iOS 13 . 0 , * )
78public struct ImageViewerRemote : View {
89 @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
1414
1515 var aspectRatio : Binding < CGFloat > ?
1616
@@ -19,15 +19,15 @@ public struct ImageViewerRemote: View {
1919
2020 @ObservedObject var loader : ImageLoader
2121
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
2424 _viewerShown = viewerShown
25- _disableCache = State ( initialValue : disableCache)
25+ self . disableCache = disableCache
2626 self . aspectRatio = aspectRatio
27- _caption = State ( initialValue : caption)
28- _closeButtonAlignment = State ( initialValue : closeButtonAlignment)
27+ self . caption = caption
28+ self . closeButtonAlignment = closeButtonAlignment
2929
30- loader = ImageLoader ( url: imageURL)
30+ loader = ImageLoader ( url: . constant ( imageURL) )
3131 }
3232
3333 @ViewBuilder
@@ -171,164 +171,6 @@ public struct ImageViewerRemote: View {
171171 }
172172}
173173
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-
332174class ImageLoader : ObservableObject {
333175 @Published var image : UIImage ?
334176 private let url : Binding < String >
@@ -366,12 +208,3 @@ class ImageLoader: ObservableObject {
366208 cancellable? . cancel ( )
367209 }
368210}
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