@@ -6,24 +6,27 @@ public struct ImageViewer: View {
66 @Binding var viewerShown : Bool
77 @Binding var image : Image
88 @Binding var imageOpt : Image ?
9+ @State var caption : Text ?
910
1011 var aspectRatio : Binding < CGFloat > ?
1112
1213 @State var dragOffset : CGSize = CGSize . zero
1314 @State var dragOffsetPredicted : CGSize = CGSize . zero
1415
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 ) {
1617 _image = image
1718 _viewerShown = viewerShown
1819 _imageOpt = . constant( nil )
1920 self . aspectRatio = aspectRatio
21+ _caption = State ( initialValue: caption)
2022 }
2123
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 ) {
2325 _image = . constant( Image ( systemName: " " ) )
2426 _imageOpt = image
2527 _viewerShown = viewerShown
2628 self . aspectRatio = aspectRatio
29+ _caption = State ( initialValue: caption)
2730 }
2831
2932 func getImage( ) -> Image {
@@ -57,26 +60,51 @@ public struct ImageViewer: View {
5760 . zIndex ( 2 )
5861
5962 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 ( )
7488
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)
76104 }
77- self . dragOffset = . zero
105+ . frame ( maxWidth : . infinity , maxHeight : . infinity )
78106 }
79- )
107+ }
80108 }
81109 . frame ( maxWidth: . infinity, maxHeight: . infinity)
82110 . 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 {
93121 }
94122}
95123
124+
96125class PinchZoomView : UIView {
97126
98127 weak var delegate : PinchZoomViewDelgate ?
0 commit comments