@@ -42,11 +42,10 @@ import AVFoundation
42
42
public final class ImageViewer : UIViewController , UIScrollViewDelegate , UIViewControllerTransitioningDelegate {
43
43
44
44
/// UI
45
- private var scrollView : UIScrollView !
46
- private var overlayView : UIView !
47
- private var closeButton : UIButton !
45
+ private var scrollView = UIScrollView ( )
46
+ private var overlayView = UIView ( )
47
+ private var closeButton = UIButton ( )
48
48
private var imageView = UIImageView ( )
49
-
50
49
private let displacedView : UIView
51
50
private var applicationWindow : UIWindow ? {
52
51
return UIApplication . sharedApplication ( ) . delegate? . window? . flatMap { $0 }
@@ -71,7 +70,7 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
71
70
private let hideCloseButtonDuration = 0.05
72
71
private let zoomDuration = 0.2
73
72
private let thresholdVelocity : CGFloat = 1000 // Based on UX experiments
74
- private let cutOffVelocity : CGFloat = 1000000 // we need some sufficiently large number, nobody can swipe faster then that
73
+ private let cutOffVelocity : CGFloat = 1000000 // we simply need some sufficiently large number, nobody can swipe faster than that
75
74
/// TRANSITIONS
76
75
private let presentTransition : ImageViewerPresentTransition
77
76
private let dismissTransition : ImageViewerDismissTransition
@@ -101,6 +100,7 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
101
100
// MARK: - Deinit
102
101
103
102
deinit {
103
+
104
104
scrollView. removeObserver ( self , forKeyPath: " contentOffset " )
105
105
}
106
106
@@ -121,6 +121,11 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
121
121
transitioningDelegate = self
122
122
modalPresentationStyle = . Custom
123
123
extendedLayoutIncludesOpaqueBars = true
124
+
125
+ overlayView. autoresizingMask = [ . None]
126
+ configureCloseButton ( )
127
+ configureImageView ( )
128
+ configureScrollView ( )
124
129
}
125
130
126
131
public required init ? ( coder aDecoder: NSCoder ) {
@@ -136,6 +141,7 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
136
141
closeButton. setImage ( closeButtonAssets. normal, forState: UIControlState . Normal)
137
142
closeButton. setImage ( closeButtonAssets. highlighted, forState: UIControlState . Highlighted)
138
143
closeButton. alpha = 0.0
144
+ closeButton. addTarget ( self , action: #selector( ImageViewer . close ( _: ) ) , forControlEvents: . TouchUpInside)
139
145
}
140
146
141
147
private func configureGestureRecognizers( ) {
@@ -154,22 +160,37 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
154
160
155
161
imageView. frame = parentViewFrameInOurCoordinateSystem
156
162
imageView. contentMode = . ScaleAspectFit
157
- view. addSubview ( imageView)
158
163
imageView. image = screenshotFromView ( displacedView)
159
164
}
160
165
161
166
private func configureScrollView( ) {
162
167
168
+ scrollView. addObserver ( self , forKeyPath: " contentOffset " , options: NSKeyValueObservingOptions . New, context: nil )
169
+ scrollView. autoresizingMask = [ . FlexibleWidth, . FlexibleHeight]
163
170
scrollView. decelerationRate = 0.5
164
171
scrollView. contentInset = UIEdgeInsetsZero
165
172
scrollView. contentOffset = CGPointZero
166
173
scrollView. contentSize = imageView. frame. size
167
174
scrollView. minimumZoomScale = 1
168
- scrollView. addObserver ( self , forKeyPath: " contentOffset " , options: NSKeyValueObservingOptions . New, context: nil )
175
+ scrollView. delegate = self
176
+ }
177
+
178
+ func createViewHierarchy( ) {
179
+
180
+ view. addSubview ( overlayView)
181
+ view. addSubview ( imageView)
182
+ view. addSubview ( scrollView)
183
+ view. addSubview ( closeButton)
169
184
}
170
185
171
186
// MARK: - View Lifecycle
172
187
188
+ public override func viewDidLoad( ) {
189
+ super. viewDidLoad ( )
190
+
191
+ createViewHierarchy ( )
192
+ }
193
+
173
194
public override func viewWillTransitionToSize( size: CGSize , withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator ) {
174
195
super. viewWillTransitionToSize ( size, withTransitionCoordinator: coordinator)
175
196
shouldRotate = true
@@ -196,32 +217,6 @@ public final class ImageViewer: UIViewController, UIScrollViewDelegate, UIViewCo
196
217
}
197
218
}
198
219
199
- public override func loadView( ) {
200
- super. loadView ( )
201
-
202
- scrollView = UIScrollView ( frame: CGRectZero)
203
- overlayView = UIView ( frame: CGRectZero)
204
- closeButton = UIButton ( frame: CGRectZero)
205
-
206
- scrollView. autoresizingMask = [ . FlexibleWidth, . FlexibleHeight]
207
- overlayView. autoresizingMask = [ . None]
208
-
209
- view. addSubview ( overlayView)
210
- view. addSubview ( scrollView)
211
- view. addSubview ( closeButton)
212
-
213
- scrollView. delegate = self
214
- closeButton. addTarget ( self , action: #selector( ImageViewer . close ( _: ) ) , forControlEvents: . TouchUpInside)
215
- }
216
-
217
- public override func viewDidLoad( ) {
218
- super. viewDidLoad ( )
219
-
220
- configureCloseButton ( )
221
- configureImageView ( )
222
- configureScrollView ( )
223
- }
224
-
225
220
// MARK: - Transitioning Delegate
226
221
227
222
public func animationControllerForPresentedController( presented: UIViewController , presentingController presenting: UIViewController , sourceController source: UIViewController ) -> UIViewControllerAnimatedTransitioning ? {
0 commit comments