-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathGalleryVC.swift
116 lines (86 loc) · 4.35 KB
/
GalleryVC.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
//
// GalleryVC.swift
// ScrollStackControllerDemo
//
// Created by Daniele Margutti on 04/10/2019.
// Copyright © 2019 ScrollStackController. All rights reserved.
//
import UIKit
public class GalleryVC: UIViewController, ScrollStackContainableController {
@IBOutlet public var collectionView: UICollectionView!
@IBOutlet public var pageControl: UIPageControl!
public var urls: [URL] = [
URL(string: "http://cdn.luxuo.com/2011/05/Aerial-view-luxury-Burj-Al-Arab.jpg")!,
URL(string: "https://mediastream.jumeirah.com/webimage/heroactual//globalassets/global/hotels-and-resorts/dubai/burj-al-arab/rooms/new-royal-two-berdoom-suite/burj-al-arab-royal-suite-staircase-5-hero.jpg")!,
URL(string: "https://mediastream.jumeirah.com/webimage/image1152x648//globalassets/global/hotels-and-resorts/dubai/burj-al-arab/rooms/new-sky-one-bedroom-suite/2019/burj-al-arab-jumeirah-sky-one-bedroom-suite-living-room-desktop.jpeg")!,
URL(string: "https://q-xx.bstatic.com/xdata/images/hotel/max500/200178877.jpg?k=229a02237c3998ac6e8b11daae254113268e779e49ab2d18964f2e97bdc947a0&o=")!
]
public static func create() -> GalleryVC {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "GalleryVC") as! GalleryVC
return vc
}
public func scrollStackRowSizeForAxis(_ axis: NSLayoutConstraint.Axis, row: ScrollStackRow, in stackView: ScrollStack) -> ScrollStack.ControllerSize? {
return .fixed(300)
}
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
reloadData()
}
public func reloadContentFromStackView(stackView: ScrollStack, row: ScrollStackRow, animated: Bool) {
}
private func reloadData() {
pageControl.numberOfPages = urls.count
collectionView.reloadData()
}
}
extension GalleryVC: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return urls.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryCell", for: indexPath) as! GalleryCell
cell.url = urls[indexPath.item]
return cell
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//let size = collectionView.bounds.size
return CGSize(width: 310, height: collectionView.bounds.size.height) // fix here
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return .zero
}
public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
pageControl.currentPage = indexPath.item
}
}
public class GalleryCell: UICollectionViewCell {
@IBOutlet public var imageView: UIImageView!
private var dataTask: URLSessionTask?
public var url: URL? {
didSet {
dataTask?.cancel()
guard let url = url else {
self.imageView.image = nil
return
}
dataTask = URLSession.shared.dataTask(with: url) { (data, _, error) in
let image = (data != nil ? UIImage(data: data!) : nil)
DispatchQueue.main.async {
self.imageView.image = image
}
}
dataTask?.resume()
}
}
public override func prepareForReuse() {
super.prepareForReuse()
self.url = nil
}
}