-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathFullScreenSlideshowViewController.swift
More file actions
132 lines (100 loc) · 3.74 KB
/
Copy pathFullScreenSlideshowViewController.swift
File metadata and controls
132 lines (100 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// FullScreenSlideshowViewController.swift
// ImageSlideshow
//
// Created by Petr Zvoníček on 31.08.15.
//
import UIKit
@objcMembers
open class FullScreenSlideshowViewController: UIViewController {
open var slideshow: ImageSlideshow = {
let slideshow = ImageSlideshow()
slideshow.zoomEnabled = true
slideshow.contentScaleMode = UIViewContentMode.scaleAspectFit
slideshow.pageIndicatorPosition = PageIndicatorPosition(horizontal: .center, vertical: .bottom)
// turns off the timer
slideshow.slideshowInterval = 0
slideshow.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
return slideshow
}()
/// Close button
open var closeButton = UIButton()
/// Close button frame
open var closeButtonFrame: CGRect?
/// Closure called on page selection
open var pageSelected: ((_ page: Int) -> Void)?
/// Index of initial image
open var initialPage: Int = 0
/// Number of images to preload
open var preload: ImagePreload?
/// Input sources to
open var inputs: [InputSource]?
/// Background color
open var backgroundColor = UIColor.black
/// Enables/disable zoom
open var zoomEnabled = true {
didSet {
slideshow.zoomEnabled = zoomEnabled
}
}
fileprivate var isInit = true
convenience init() {
self.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = .custom
if #available(iOS 13.0, *) {
// Use KVC to set the value to preserve backwards compatiblity with Xcode < 11
self.setValue(true, forKey: "modalInPresentation")
}
}
override open func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = backgroundColor
slideshow.backgroundColor = backgroundColor
if let preload = preload {
slideshow.preload = preload
}
if let inputs = inputs {
slideshow.setImageInputs(inputs)
}
view.addSubview(slideshow)
// close button configuration
closeButton.setImage(UIImage(named: "ic_cross_white", in: Bundle(for: type(of: self)), compatibleWith: nil), for: UIControlState())
closeButton.addTarget(self, action: #selector(FullScreenSlideshowViewController.close), for: UIControlEvents.touchUpInside)
view.addSubview(closeButton)
}
override open var prefersStatusBarHidden: Bool {
return true
}
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isInit {
isInit = false
slideshow.setCurrentPage(initialPage, animated: false)
}
}
override open func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
slideshow.slideshowItems.forEach { $0.cancelPendingLoad() }
// Prevents broken dismiss transition when image is zoomed in
slideshow.currentSlideshowItem?.zoomOut()
}
open override func viewDidLayoutSubviews() {
if !isBeingDismissed {
let safeAreaInsets: UIEdgeInsets
if #available(iOS 11.0, *) {
safeAreaInsets = view.safeAreaInsets
} else {
safeAreaInsets = UIEdgeInsets.zero
}
closeButton.frame = closeButtonFrame ?? CGRect(x: max(10, safeAreaInsets.left), y: max(10, safeAreaInsets.top), width: 40, height: 40)
}
slideshow.frame = view.frame
}
func close() {
// if pageSelected closure set, send call it with current page
if let pageSelected = pageSelected {
pageSelected(slideshow.currentPage)
}
dismiss(animated: true, completion: nil)
}
}