-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathMenuViewController.swift
298 lines (245 loc) · 11.2 KB
/
MenuViewController.swift
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//
// MenuViewController.swift
// PhotoBrowserDemo
//
// Created by Eduardo Callado on 11/27/16.
//
//
import UIKit
class MenuViewController: UITableViewController, IDMPhotoBrowserDelegate { }
// MARK: View Lifecycle
extension MenuViewController {
override func viewDidLoad() {
self.setupTableViewFooterView()
}
}
// MARK: Layout
extension MenuViewController {
override var prefersStatusBarHidden: Bool {
return true
}
}
// MARK: General
extension MenuViewController {
func setupTableViewFooterView() {
let tableViewFooter: UIView = UIView(frame: CGRect.init(x: 0, y: 0, width: 320, height: 426 * 0.9 + 40))
let buttonWithImageOnScreen1 = UIButton(type: .custom)
buttonWithImageOnScreen1.frame = CGRect.init(x: 15, y: 0, width: 640/3 * 0.9, height: 426/2 * 0.9)
buttonWithImageOnScreen1.tag = 101
buttonWithImageOnScreen1.adjustsImageWhenHighlighted = false
buttonWithImageOnScreen1.setImage(UIImage.init(named: "photo1m.jpg"), for: .normal)
buttonWithImageOnScreen1.imageView?.contentMode = .scaleAspectFill
buttonWithImageOnScreen1.backgroundColor = UIColor.black
buttonWithImageOnScreen1.addTarget(self, action: #selector(buttonWithImageOnScreenPressed(sender:)), for: .touchUpInside)
tableViewFooter.addSubview(buttonWithImageOnScreen1)
let buttonWithImageOnScreen2 = UIButton(type: .custom)
buttonWithImageOnScreen2.frame = CGRect.init(x: 15, y: 426/2 * 0.9 + 20, width: 640/3 * 0.9, height: 426/2 * 0.9)
buttonWithImageOnScreen2.tag = 102
buttonWithImageOnScreen2.adjustsImageWhenHighlighted = false
buttonWithImageOnScreen2.setImage(UIImage.init(named: "photo3m.jpg"), for: .normal)
buttonWithImageOnScreen2.imageView?.contentMode = .scaleAspectFill
buttonWithImageOnScreen2.backgroundColor = UIColor.black
buttonWithImageOnScreen2.addTarget(self, action: #selector(buttonWithImageOnScreenPressed(sender:)), for: .touchUpInside)
tableViewFooter.addSubview(buttonWithImageOnScreen2)
self.tableView.tableFooterView = tableViewFooter;
}
}
// MARK: Actions
extension MenuViewController {
func buttonWithImageOnScreenPressed(sender: AnyObject) {
let buttonSender = sender as? UIButton
// Create an array to store IDMPhoto objects
var photos: [IDMPhoto] = []
var photo: IDMPhoto
if buttonSender?.tag == 101 {
let path_photo1l = [Bundle.main.path(forResource: "photo1l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo1l).first as! IDMPhoto
photo.caption = "Grotto of the Madonna"
photos.append(photo)
}
let path_photo3l = [Bundle.main.path(forResource: "photo3l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo3l).first as! IDMPhoto
photo.caption = "York Floods"
photos.append(photo)
let path_photo2l = [Bundle.main.path(forResource: "photo2l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo2l).first as! IDMPhoto
photo.caption = "The London Eye is a giant Ferris wheel situated on the banks of the River Thames, in London, England."
photos.append(photo)
let path_photo4l = [Bundle.main.path(forResource: "photo4l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo4l).first as! IDMPhoto
photo.caption = "Campervan";
photos.append(photo)
if buttonSender?.tag == 102 {
let path_photo1l = [Bundle.main.path(forResource: "photo1l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo1l).first as! IDMPhoto
photo.caption = "Grotto of the Madonna";
photos.append(photo)
}
// Create and setup browser
let browser: IDMPhotoBrowser = IDMPhotoBrowser(photos: photos, animatedFrom: buttonSender) // using initWithPhotos:animatedFromView:
browser.delegate = self
browser.displayActionButton = false
browser.displayArrowButton = true
browser.displayCounterLabel = true
browser.usePopAnimation = true
browser.scaleImage = buttonSender?.currentImage
browser.dismissOnTouch = true
// Show
self.present(browser, animated: true, completion: nil)
}
}
// MARK: TableView Data Source
extension MenuViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
return 4
case 2:
return 0
default:
return 0
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return "Single photo"
case 1:
return "Multiple photos"
case 2:
return "Photos on screen"
default:
return ""
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Create
let cellIdentifier = "Cell";
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: cellIdentifier)
}
// Configure
if indexPath.section == 0 {
cell?.textLabel?.text = "Local photo"
} else if indexPath.section == 1 {
switch indexPath.row {
case 0:
cell?.textLabel?.text = "Local photos"
case 1:
cell?.textLabel?.text = "Photos from Flickr"
case 2:
cell?.textLabel?.text = "Photos from Flickr - Custom"
case 3:
cell?.textLabel?.text = "Video"
default:
break
}
}
return cell!
}
}
// MARK: TableView Delegate
extension MenuViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// Create an array to store IDMPhoto objects
var photos: [IDMPhoto] = []
var photo: IDMPhoto
if indexPath.section == 0 { // Local photo
let path_photo2l = [Bundle.main.path(forResource: "photo2l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo2l).first as! IDMPhoto
photo.caption = "The London Eye is a giant Ferris wheel situated on the banks of the River Thames, in London, England."
photos.append(photo)
}
else if indexPath.section == 1 { // Multiple photos
if indexPath.row == 0 { // Local Photos
let path_photo1l = [Bundle.main.path(forResource: "photo1l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo1l).first as! IDMPhoto
photo.caption = "Grotto of the Madonna"
photos.append(photo)
let path_photo2l = [Bundle.main.path(forResource: "photo2l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo2l).first as! IDMPhoto
photo.caption = "The London Eye is a giant Ferris wheel situated on the banks of the River Thames, in London, England."
photos.append(photo)
let path_photo3l = [Bundle.main.path(forResource: "photo3l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo3l).first as! IDMPhoto
photo.caption = "York Floods"
photos.append(photo)
let path_photo4l = [Bundle.main.path(forResource: "photo4l", ofType: "jpg")]
photo = IDMPhoto.photos(withFilePaths:path_photo4l).first as! IDMPhoto
photo.caption = "Campervan";
photos.append(photo)
} else if indexPath.row == 1 || indexPath.row == 2 { // Photos from Flickr or Flickr - Custom
let photosWithURLArray = [NSURL.init(string: "http://farm4.static.flickr.com/3567/3523321514_371d9ac42f_b.jpg"),
NSURL.init(string: "http://farm4.static.flickr.com/3629/3339128908_7aecabc34b_b.jpg"),
NSURL.init(string: "http://farm4.static.flickr.com/3364/3338617424_7ff836d55f_b.jpg"),
NSURL.init(string: "http://farm4.static.flickr.com/3590/3329114220_5fbc5bc92b_b.jpg")]
let photosWithURL: [IDMPhoto] = IDMPhoto.photos(withURLs: photosWithURLArray) as! [IDMPhoto]
photos = photosWithURL
} else if indexPath.row == 3 { // Videos
let video1 = IDMPhoto(video: URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)!
video1.caption = "Big Buck Bunny — by THE PEACH OPEN MOVIE PROJECT"
let photo1 = IDMPhoto(url: URL(string: "http://farm4.static.flickr.com/3590/3329114220_5fbc5bc92b_b.jpg")!)!
photo1.caption = "A standard picture separating two videos"
let video2 = IDMPhoto(video: URL(string: "https://staging.coverr.co/s3/mp4/Playful.mp4")!)!
video2.caption = "A cover coming straight from coverr.co for an example"
let videos: [IDMPhoto] = [
video1,
photo1,
video2
]
photos = videos
}
}
// Create and setup browser
let browser = IDMPhotoBrowser.init(photos: photos)
browser?.delegate = self
if indexPath.section == 1 { // Multiple photos
if indexPath.row == 1 || indexPath.row == 3 { // Photos from Flickr
browser?.displayCounterLabel = true
browser?.displayActionButton = false
} else if indexPath.row == 2 { // Photos from Flickr - Custom
browser?.actionButtonTitles = ["Option 1", "Option 2", "Option 3", "Option 4"]
browser?.displayCounterLabel = true
browser?.useWhiteBackgroundColor = true
browser?.leftArrowImage = UIImage.init(named: "IDMPhotoBrowser_customArrowLeft.png")
browser?.rightArrowImage = UIImage.init(named: "IDMPhotoBrowser_customArrowRight.png")
browser?.leftArrowSelectedImage = UIImage.init(named: "IDMPhotoBrowser_customArrowLeftSelected.png")
browser?.rightArrowSelectedImage = UIImage.init(named: "IDMPhotoBrowser_customArrowRightSelected.png")
browser?.doneButtonImage = UIImage.init(named: "IDMPhotoBrowser_customDoneButton.png")
browser?.view.tintColor = UIColor.orange
browser?.progressTintColor = UIColor.orange
browser?.trackTintColor = UIColor.init(white: 0.8, alpha: 1)
}
}
// Show
present(browser!, animated: true, completion: nil)
tableView.deselectRow(at: indexPath, animated: true)
}
}
// MARK: IDMPhotoBrowser Delegate
extension MenuViewController {
func photoBrowser(_ photoBrowser: IDMPhotoBrowser!, didShowPhotoAt index: UInt) {
let photo: IDMPhoto = photoBrowser.photo(at: index) as! IDMPhoto
print("Did show photoBrowser with photo index: \(index), photo caption: \(photo.caption)")
}
func photoBrowser(_ photoBrowser: IDMPhotoBrowser!, willDismissAtPageIndex index: UInt) {
let photo: IDMPhoto = photoBrowser.photo(at: index) as! IDMPhoto
print("Will dismiss photoBrowser with photo index: \(index), photo caption: \(photo.caption)")
}
func photoBrowser(_ photoBrowser: IDMPhotoBrowser!, didDismissAtPageIndex index: UInt) {
let photo: IDMPhoto = photoBrowser.photo(at: index) as! IDMPhoto
print("Did dismiss photoBrowser with photo index: \(index), photo caption: \(photo.caption)")
}
func photoBrowser(_ photoBrowser: IDMPhotoBrowser!, didDismissActionSheetWithButtonIndex buttonIndex: UInt, photoIndex: UInt) {
let photo: IDMPhoto = photoBrowser.photo(at: buttonIndex) as! IDMPhoto
print("Did dismiss photoBrowser with photo index: \(buttonIndex), photo caption: \(photo.caption)")
UIAlertView(title: "Option \(buttonIndex+1)", message: nil, delegate: nil, cancelButtonTitle: "OK").show()
}
}