@@ -6,24 +6,27 @@ public struct ImageViewer: View {
6
6
@Binding var viewerShown : Bool
7
7
@Binding var image : Image
8
8
@Binding var imageOpt : Image ?
9
+ @State var caption : Text ?
9
10
10
11
var aspectRatio : Binding < CGFloat > ?
11
12
12
13
@State var dragOffset : CGSize = CGSize . zero
13
14
@State var dragOffsetPredicted : CGSize = CGSize . zero
14
15
15
- public init ( image: Binding < Image > , viewerShown: Binding < Bool > , aspectRatio: Binding < CGFloat > ? = nil ) {
16
+ public init ( image: Binding < Image > , viewerShown: Binding < Bool > , aspectRatio: Binding < CGFloat > ? = nil , caption : Text ? = nil ) {
16
17
_image = image
17
18
_viewerShown = viewerShown
18
19
_imageOpt = . constant( nil )
19
20
self . aspectRatio = aspectRatio
21
+ _caption = State ( initialValue: caption)
20
22
}
21
23
22
- public init ( image: Binding < Image ? > , viewerShown: Binding < Bool > , aspectRatio: Binding < CGFloat > ? = nil ) {
24
+ public init ( image: Binding < Image ? > , viewerShown: Binding < Bool > , aspectRatio: Binding < CGFloat > ? = nil , caption : Text ? = nil ) {
23
25
_image = . constant( Image ( systemName: " " ) )
24
26
_imageOpt = image
25
27
_viewerShown = viewerShown
26
28
self . aspectRatio = aspectRatio
29
+ _caption = State ( initialValue: caption)
27
30
}
28
31
29
32
func getImage( ) -> Image {
@@ -57,26 +60,51 @@ public struct ImageViewer: View {
57
60
. zIndex ( 2 )
58
61
59
62
VStack {
60
- self . getImage ( )
61
- . resizable ( )
62
- . aspectRatio ( self . aspectRatio? . wrappedValue, contentMode: . fit)
63
- . offset ( x: self . dragOffset. width, y: self . dragOffset. height)
64
- . rotationEffect ( . init( degrees: Double ( self . dragOffset. width / 30 ) ) )
65
- . pinchToZoom ( )
66
- . gesture ( DragGesture ( )
67
- . onChanged { value in
68
- self . dragOffset = value. translation
69
- self . dragOffsetPredicted = value. predictedEndTranslation
70
- }
71
- . onEnded { value in
72
- if ( ( abs ( self . dragOffset. height) + abs( self . dragOffset. width) > 570 ) || ( ( abs ( self . dragOffsetPredicted. height) ) / ( abs ( self . dragOffset. height) ) > 3 ) || ( ( abs ( self . dragOffsetPredicted. width) ) / ( abs ( self . dragOffset. width) ) ) > 3 ) {
73
- self . viewerShown = false
63
+ ZStack {
64
+ self . getImage ( )
65
+ . resizable ( )
66
+ . aspectRatio ( self . aspectRatio? . wrappedValue, contentMode: . fit)
67
+ . offset ( x: self . dragOffset. width, y: self . dragOffset. height)
68
+ . rotationEffect ( . init( degrees: Double ( self . dragOffset. width / 30 ) ) )
69
+ . pinchToZoom ( )
70
+ . gesture ( DragGesture ( )
71
+ . onChanged { value in
72
+ self . dragOffset = value. translation
73
+ self . dragOffsetPredicted = value. predictedEndTranslation
74
+ }
75
+ . onEnded { value in
76
+ if ( ( abs ( self . dragOffset. height) + abs( self . dragOffset. width) > 570 ) || ( ( abs ( self . dragOffsetPredicted. height) ) / ( abs ( self . dragOffset. height) ) > 3 ) || ( ( abs ( self . dragOffsetPredicted. width) ) / ( abs ( self . dragOffset. width) ) ) > 3 ) {
77
+ self . viewerShown = false
78
+
79
+ return
80
+ }
81
+ self . dragOffset = . zero
82
+ }
83
+ )
84
+
85
+ if ( self . caption != nil ) {
86
+ VStack {
87
+ Spacer ( )
74
88
75
- return
89
+ VStack {
90
+ Spacer ( )
91
+
92
+ HStack {
93
+ Spacer ( )
94
+
95
+ self . caption
96
+ . foregroundColor ( . white)
97
+ . multilineTextAlignment ( . center)
98
+
99
+ Spacer ( )
100
+ }
101
+ }
102
+ . padding ( )
103
+ . frame ( maxWidth: . infinity, maxHeight: . infinity)
76
104
}
77
- self . dragOffset = . zero
105
+ . frame ( maxWidth : . infinity , maxHeight : . infinity )
78
106
}
79
- )
107
+ }
80
108
}
81
109
. frame ( maxWidth: . infinity, maxHeight: . infinity)
82
110
. background ( Color ( red: 0.12 , green: 0.12 , blue: 0.12 , opacity: ( 1.0 - Double( abs ( self . dragOffset. width) + abs( self . dragOffset. height) ) / 1000 ) ) . edgesIgnoringSafeArea ( . all) )
@@ -93,6 +121,7 @@ public struct ImageViewer: View {
93
121
}
94
122
}
95
123
124
+
96
125
class PinchZoomView : UIView {
97
126
98
127
weak var delegate : PinchZoomViewDelgate ?
0 commit comments