Skip to content

Commit 68b7043

Browse files
committed
just use the rootVC instead of specifying which vc is going to use.
1 parent 2650a80 commit 68b7043

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

Example/Demo/GalleryViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ extension GalleryViewController:UICollectionViewDataSource {
8282

8383
// Setup Image Viewer
8484
cell.imageView.setupImageViewer(
85-
with: self,
8685
images: images,
8786
initialIndex: indexPath.item)
8887
return cell

Sources/ImageViewerController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ class ImageViewerController : UIViewController, UIGestureRecognizerDelegate {
271271
}
272272
}
273273

274-
275274
extension ImageViewerController:UIScrollViewDelegate {
276275

277276
func viewForZooming(in scrollView: UIScrollView) -> UIView? {

Sources/UIImageView_Extensions.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ extension UIImageView {
44

55
// Data holder tap recognizer
66
private class TapWithDataRecognizer:UITapGestureRecognizer {
7-
unowned var viewController:UIViewController!
87
var imageDatasource:ImageDataSource?
98
var initialIndex:Int = 0
109
var options:[ImageViewerOption] = []
1110
}
1211

12+
private var vc:UIViewController? {
13+
return UIApplication.shared.keyWindow?.rootViewController
14+
}
15+
1316
public func setupImageViewer(
14-
with vc:UIViewController, options:[ImageViewerOption] = []) {
15-
setup(with: vc, datasource: nil, options: options)
17+
options:[ImageViewerOption] = []) {
18+
setup(datasource: nil, options: options)
1619
}
1720

1821
public func setupImageViewer(
19-
with vc:UIViewController,
20-
url:URL, initialIndex:Int = 0,
22+
url:URL,
23+
initialIndex:Int = 0,
2124
placeholder: UIImage? = nil,
2225
options:[ImageViewerOption] = []) {
2326

@@ -26,31 +29,29 @@ extension UIImageView {
2629
ImageItem.url($0, placeholder: placeholder ?? image)
2730
})
2831
setup(
29-
with: vc,
3032
datasource: datasource,
3133
initialIndex: initialIndex,
3234
options: options)
3335
}
3436

3537
public func setupImageViewer(
36-
with vc:UIViewController,
37-
images:[UIImage], initialIndex:Int = 0,
38+
images:[UIImage],
39+
initialIndex:Int = 0,
3840
options:[ImageViewerOption] = []) {
3941

4042
let datasource = SimpleImageDatasource(
4143
imageItems: images.compactMap {
4244
ImageItem.image($0)
4345
})
4446
setup(
45-
with: vc,
4647
datasource: datasource,
4748
initialIndex: initialIndex,
4849
options: options)
4950
}
5051

5152
public func setupImageViewer(
52-
with vc:UIViewController,
53-
urls:[URL], initialIndex:Int = 0,
53+
urls:[URL],
54+
initialIndex:Int = 0,
5455
options:[ImageViewerOption] = [],
5556
placeholder: UIImage? = nil) {
5657

@@ -59,18 +60,23 @@ extension UIImageView {
5960
ImageItem.url($0, placeholder: placeholder)
6061
})
6162
setup(
62-
with: vc,
6363
datasource: datasource,
6464
initialIndex: initialIndex,
6565
options: options)
6666
}
6767

68-
public func setupImageViewer(with vc:UIViewController, datasource:ImageDataSource, initialIndex:Int = 0, options:[ImageViewerOption] = []) {
69-
setup(with: vc, datasource: datasource, initialIndex: initialIndex, options: options)
68+
public func setupImageViewer(
69+
datasource:ImageDataSource,
70+
initialIndex:Int = 0,
71+
options:[ImageViewerOption] = []) {
72+
73+
setup(
74+
datasource: datasource,
75+
initialIndex: initialIndex,
76+
options: options)
7077
}
7178

7279
private func setup(
73-
with vc:UIViewController,
7480
datasource:ImageDataSource?,
7581
initialIndex:Int = 0,
7682
options:[ImageViewerOption] = []) {
@@ -90,7 +96,6 @@ extension UIImageView {
9096
_tapRecognizer.numberOfTapsRequired = 1
9197

9298
// Pass the Data
93-
_tapRecognizer.viewController = vc
9499
_tapRecognizer.imageDatasource = datasource
95100
_tapRecognizer.initialIndex = initialIndex
96101
_tapRecognizer.options = options
@@ -111,6 +116,6 @@ extension UIImageView {
111116
imageCarousel.options = sender.options
112117
imageCarousel.modalPresentationStyle = .overFullScreen
113118
imageCarousel.modalPresentationCapturesStatusBarAppearance = true
114-
sender.viewController.present(imageCarousel, animated: false, completion: nil)
119+
vc?.present(imageCarousel, animated: false, completion: nil)
115120
}
116121
}

0 commit comments

Comments
 (0)