forked from amirdew/CollectionViewPagingLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutDesignerIntroViewController.swift
More file actions
130 lines (106 loc) · 4.15 KB
/
LayoutDesignerIntroViewController.swift
File metadata and controls
130 lines (106 loc) · 4.15 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
//
// LayoutDesignerIntroViewController.swift
// PagingLayoutSamples
//
// Created by Amir on 09/07/2020.
// Copyright © 2020 Amir Khorsandi. All rights reserved.
//
import UIKit
import CollectionViewPagingLayout
class LayoutDesignerIntroViewController: UIViewController {
// MARK: Properties
var viewModel: LayoutDesignerIntroViewModel? {
didSet {
refreshViews()
}
}
private var collectionView: UICollectionView!
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .clear
setupCollectionView()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
open()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
DispatchQueue.main.async { [weak self] in
self?.collectionView?.performBatchUpdates({ [weak self] in
self?.collectionView?.collectionViewLayout.invalidateLayout()
})
}
}
// MARK: Private functions
private func setupCollectionView() {
collectionView = UICollectionView(frame: .zero, collectionViewLayout: CollectionViewPagingLayout())
collectionView.isPagingEnabled = true
collectionView.register(LayoutDesignerIntroCell.self)
collectionView.dataSource = self
collectionView.layer.cornerRadius = 44
collectionView.clipsToBounds = true
collectionView.backgroundColor = .white
collectionView.isScrollEnabled = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.alpha = 0
let darkOverlay = UIView()
darkOverlay.backgroundColor = UIColor.black.withAlphaComponent(0.59)
view.fill(with: darkOverlay)
let tap = UITapGestureRecognizer(target: self, action: #selector(close))
darkOverlay.addGestureRecognizer(tap)
darkOverlay.alpha = 0
view.addSubview(collectionView)
collectionView.widthAnchor.constraint(equalToConstant: 950).isActive = true
collectionView.heightAnchor.constraint(equalToConstant: 600).isActive = true
darkOverlay.center(to: collectionView)
}
private func refreshViews() {
collectionView?.reloadData()
}
private func open() {
UIView.animate(withDuration: 0.25) { [weak self] in
self?.view.subviews.forEach {
$0.alpha = 1
}
}
}
@objc private func close() {
UIView.animate(withDuration: 0.25, animations: { [weak self] in
self?.view.subviews.forEach {
$0.alpha = 0
}
}, completion: { [weak self] _ in
self?.view.removeFromSuperview()
self?.didMove(toParent: nil)
})
}
}
extension LayoutDesignerIntroViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
viewModel?.introPages.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(for: indexPath) as LayoutDesignerIntroCell
cell.introInfo = viewModel?.introPages[indexPath.row]
cell.delegate = self
return cell
}
}
extension LayoutDesignerIntroViewController: LayoutDesignerIntroCellDelegate {
func layoutDesignerIntroCell(_ cell: LayoutDesignerIntroCell, onLeftButtonTouched button: UIButton) {
if collectionView.indexPath(for: cell)?.item == 0 {
close()
return
}
(collectionView.collectionViewLayout as? CollectionViewPagingLayout)?.goToPreviousPage()
}
func layoutDesignerIntroCell(_ cell: LayoutDesignerIntroCell, onRightButtonTouched button: UIButton) {
if collectionView.indexPath(for: cell)?.item == viewModel.map({ $0.introPages.count - 1 }) {
close()
return
}
(collectionView.collectionViewLayout as? CollectionViewPagingLayout)?.goToNextPage()
}
}