Skip to content

Commit 22aa66d

Browse files
committed
Removed binding, state from variables; updated README
1 parent ac3a526 commit 22aa66d

File tree

4 files changed

+49
-203
lines changed

4 files changed

+49
-203
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ let package = Package(
2727
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2828
.target(
2929
name: "ImageViewer",
30-
dependencies: ["URLImage"]),
30+
dependencies: []),
3131
.target(
3232
name: "ImageViewerRemote",
33-
dependencies: ["URLImage"])
33+
dependencies: ["ImageViewer", "URLImage"])
3434
]
3535
)

README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ File > Swift Packages > Add Package Dependancy
2323
2424
### Local Image:
2525

26-
The `image` parameter accepts `Binding<Image>` in all versions. As of 1.0.20, it also accepts `Binding<Image?>`
26+
The `image` parameter accepts `Image` or `Image?` in versions >= 3.0.0.
27+
<details>
28+
<summary>Previous Versions</summary>
29+
<br>
30+
31+
All versions <3.0.0 accept `Binding<Image>`. >=1.0.20 and <3.0.0 also accept `Binding<Image?>`.
32+
</details>
2733

2834
```Swift
2935
import ImageViewer
@@ -37,14 +43,20 @@ struct ContentView: View {
3743
Text("Example!")
3844
}
3945
.frame(maxWidth: .infinity, maxHeight: .infinity)
40-
.overlay(ImageViewer(image: self.$image, viewerShown: self.$showImageViewer))
46+
.overlay(ImageViewer(image: self.image, viewerShown: self.$showImageViewer))
4147
}
4248
}
4349
```
4450

4551
### Remote Image:
4652

47-
The `imageURL` parameter accepts `Binding<String>`
53+
The `imageURL` parameter accepts `String`
54+
<details>
55+
<summary>Previous Versions</summary>
56+
<br>
57+
58+
All versions <3.0.0 accept `Binding<String>`.
59+
</details>
4860

4961
```Swift
5062
import ImageViewerRemote
@@ -58,7 +70,7 @@ struct ContentView: View {
5870
Text("Example!")
5971
}
6072
.frame(maxWidth: .infinity, maxHeight: .infinity)
61-
.overlay(ImageViewerRemote(imageURL: self.$imgURL, viewerShown: self.$showImageViewer))
73+
.overlay(ImageViewerRemote(imageURL: self.imgURL, viewerShown: self.$showImageViewer))
6274
}
6375
}
6476
```

Sources/ImageViewer/ImageViewer.swift

+20-19
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@ import UIKit
44
@available(iOS 13.0, *)
55
public struct ImageViewer: View {
66
@Binding var viewerShown: Bool
7-
@Binding var image: Image
8-
@Binding var imageOpt: Image?
9-
@State var caption: Text?
10-
@State var closeButtonAlignment: CloseButtonAlignment? = CloseButtonAlignment.topLeft
7+
var image: Image
8+
var imageOpt: Image?
9+
var caption: Text?
10+
var closeButtonAlignment: CloseButtonAlignment? = CloseButtonAlignment.topLeft
1111

1212
var aspectRatio: Binding<CGFloat>?
1313

1414
@State var dragOffset: CGSize = CGSize.zero
1515
@State var dragOffsetPredicted: CGSize = CGSize.zero
1616

17-
public init(image: Binding<Image>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil, closeButtonAlignment: CloseButtonAlignment?) {
18-
_image = image
17+
public init(image: Image, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil, closeButtonAlignment: CloseButtonAlignment?) {
18+
self.image = image
19+
self.imageOpt = nil
20+
1921
_viewerShown = viewerShown
20-
_imageOpt = .constant(nil)
2122
self.aspectRatio = aspectRatio
22-
_caption = State(initialValue: caption)
23-
_closeButtonAlignment = State(initialValue: closeButtonAlignment)
23+
self.caption = caption
24+
self.closeButtonAlignment = closeButtonAlignment
2425
}
2526

26-
public init(image: Binding<Image?>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil, closeButtonAlignment: CloseButtonAlignment?) {
27+
public init(image: Image?, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil, closeButtonAlignment: CloseButtonAlignment?) {
28+
self.image = Image(systemName: "")
29+
self.imageOpt = image
2730

28-
_image = .constant(Image(systemName: ""))
29-
_imageOpt = image
3031
_viewerShown = viewerShown
3132
self.aspectRatio = aspectRatio
32-
_caption = State(initialValue: caption)
33-
_closeButtonAlignment = State(initialValue: closeButtonAlignment)
33+
self.caption = caption
34+
self.closeButtonAlignment = closeButtonAlignment
3435
}
3536

3637
func getImage() -> Image {
@@ -148,7 +149,7 @@ public struct ImageViewer: View {
148149
}
149150

150151

151-
class PinchZoomView: UIView {
152+
public class PinchZoomView: UIView {
152153

153154
weak var delegate: PinchZoomViewDelgate?
154155

@@ -230,7 +231,7 @@ class PinchZoomView: UIView {
230231

231232
}
232233

233-
protocol PinchZoomViewDelgate: AnyObject {
234+
public protocol PinchZoomViewDelgate: AnyObject {
234235
func pinchZoomView(_ pinchZoomView: PinchZoomView, didChangePinching isPinching: Bool)
235236
func pinchZoomView(_ pinchZoomView: PinchZoomView, didChangeScale scale: CGFloat)
236237
func pinchZoomView(_ pinchZoomView: PinchZoomView, didChangeAnchor anchor: UnitPoint)
@@ -281,21 +282,21 @@ struct PinchZoom: UIViewRepresentable {
281282
}
282283
}
283284

284-
struct PinchToZoom: ViewModifier {
285+
public struct PinchToZoom: ViewModifier {
285286
@State var scale: CGFloat = 1.0
286287
@State var anchor: UnitPoint = .center
287288
@State var offset: CGSize = .zero
288289
@State var isPinching: Bool = false
289290

290-
func body(content: Content) -> some View {
291+
public func body(content: Content) -> some View {
291292
content
292293
.scaleEffect(scale, anchor: anchor)
293294
.offset(offset)
294295
.overlay(PinchZoom(scale: $scale, anchor: $anchor, offset: $offset, isPinching: $isPinching))
295296
}
296297
}
297298

298-
extension View {
299+
public extension View {
299300
func pinchToZoom() -> some View {
300301
self.modifier(PinchToZoom())
301302
}

Sources/ImageViewerRemote/ImageViewerRemote.swift

+11-178
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import SwiftUI
22
import UIKit
33
import URLImage
44
import Combine
5+
import ImageViewer
56

67
@available(iOS 13.0, *)
78
public 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-
332174
class 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

Comments
 (0)