-
Notifications
You must be signed in to change notification settings - Fork 533
/
ExpandingViewController.swift
132 lines (101 loc) · 4.76 KB
/
ExpandingViewController.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
//
// PageViewController.swift
// TestCollectionView
//
// Created by Alex K. on 05/05/16.
// Copyright © 2016 Alex K. All rights reserved.
//
import UIKit
/// UIViewController with UICollectionView with custom transition method
open class ExpandingViewController: UIViewController {
/// The default size to use for cells. Height of open cell state
open var itemSize = CGSize(width: 256, height: 460)
/// The collection view object managed by this view controller.
open var collectionView: UICollectionView?
fileprivate var transitionDriver: TransitionDriver?
/// Index of current cell
open var currentIndex: Int {
guard let collectionView = self.collectionView else { return 0 }
let startOffset = (collectionView.bounds.size.width - itemSize.width) / 2
guard let collectionLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return 0
}
let minimumLineSpacing = collectionLayout.minimumLineSpacing
let a = collectionView.contentOffset.x + startOffset + itemSize.width / 2
let b = itemSize.width + minimumLineSpacing
return Int(a / b)
}
}
// MARK: life cicle
extension ExpandingViewController {
open override func viewDidLoad() {
super.viewDidLoad()
commonInit()
}
}
// MARK: Transition
public extension ExpandingViewController {
/**
Pushes a view controller onto the receiver’s stack and updates the display with custom animation.
- parameter viewController: The table view controller to push onto the stack.
*/
func pushToViewController(_ viewController: ExpandingTableViewController) {
guard let collectionView = self.collectionView,
let navigationController = self.navigationController else {
return
}
viewController.transitionDriver = transitionDriver
let insets = viewController.automaticallyAdjustsScrollViewInsets
let tabBarHeight = insets == true ? navigationController.navigationBar.frame.size.height : 0
let stausBarHeight = insets == true ? UIApplication.shared.statusBarFrame.size.height : 0
let backImage = getBackImage(viewController, headerHeight: viewController.headerHeight)
transitionDriver?.pushTransitionAnimationIndex(currentIndex,
collecitionView: collectionView,
backImage: backImage,
headerHeight: viewController.headerHeight,
insets: tabBarHeight + stausBarHeight) { headerView in
viewController.tableView.tableHeaderView = headerView
self.navigationController?.pushViewController(viewController, animated: false)
}
}
}
// MARK: create
extension ExpandingViewController {
fileprivate func commonInit() {
let layout = PageCollectionLayout(itemSize: itemSize)
collectionView = PageCollectionView.createOnView(view,
layout: layout,
height: itemSize.height,
dataSource: self,
delegate: self)
if #available(iOS 10.0, *) {
collectionView?.isPrefetchingEnabled = false
}
transitionDriver = TransitionDriver(view: view)
}
}
// MARK: Helpers
extension ExpandingViewController {
fileprivate func getBackImage(_ viewController: UIViewController, headerHeight: CGFloat) -> UIImage? {
let imageSize = CGSize(width: viewController.view.bounds.width, height: viewController.view.bounds.height - headerHeight)
let imageFrame = CGRect(origin: CGPoint(x: 0, y: 0), size: imageSize)
return viewController.view.takeSnapshot(imageFrame)
}
}
// MARK: UICollectionViewDataSource
extension ExpandingViewController: UICollectionViewDataSource, UICollectionViewDelegate {
open func collectionView(_: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt _: IndexPath) {
guard case let cell as BasePageCollectionCell = cell else {
return
}
cell.configureCellViewConstraintsWithSize(itemSize)
}
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
open func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
fatalError("need emplementation in subclass")
}
open func collectionView(_: UICollectionView, cellForItemAt _: IndexPath) -> UICollectionViewCell {
fatalError("need emplementation in subclass")
}
}