-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOkPagerView.swift
246 lines (197 loc) · 7.82 KB
/
OkPagerView.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
//
// OkPagerView.swift
// OkDataSources
//
// Created by Roberto Frontado on 20/2/16.
// Copyright © 2016 Roberto Frontado. All rights reserved.
//
import UIKit
public protocol OkPagerViewDataSource: class {
func viewControllerAtIndex(_ index: Int) -> UIViewController?
func numberOfPages() -> Int?
}
public protocol OkPagerViewDelegate: class {
func onPageSelected(_ viewController: UIViewController, index: Int)
}
public protocol OkPagerViewAnimationDelegate: class {
func onScrolling(_ viewController: UIViewController, index: Int, offset: CGFloat)
}
open class OkPagerView: UIView, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate {
@IBOutlet var pageControl: UIPageControl?
fileprivate var pageViewController: UIPageViewController!
open fileprivate(set) var currentIndex = 0
open var callFirstItemOnCreated = true
open weak var dataSource: OkPagerViewDataSource? {
didSet {
reloadData()
}
}
open weak var delegate: OkPagerViewDelegate?
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addPagerViewController()
}
override init(frame: CGRect) {
super.init(frame: frame)
addPagerViewController()
}
override open func layoutSubviews() {
super.layoutSubviews()
addPagerViewController()
}
// MARK: - Private methods
fileprivate func addPagerViewController() {
if pageViewController != nil {
return
}
pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageViewController.dataSource = self
pageViewController.delegate = self
self.parentViewController?.addChildViewController(pageViewController)
addPagerView(pageViewController.view)
pageViewController.didMove(toParentViewController: self.parentViewController)
pageControl?.currentPage = 0
for view in pageViewController.view.subviews {
if let scrollView = view as? UIScrollView {
scrollView.delegate = self
}
}
}
fileprivate func addPagerView(_ pagerView: UIView) {
pagerView.frame = self.bounds
self.addSubview(pagerView)
let constTop = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: pagerView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let constBottom = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: pagerView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let constLeft = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: pagerView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
let constRight = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: pagerView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
self.addConstraints([constTop, constBottom, constLeft, constRight])
}
fileprivate func getViewControllerAtIndex(_ index: Int) -> UIViewController? {
if (getNumberOfPages() == 0
|| index >= getNumberOfPages())
{
return nil
}
// Create a new View Controller and pass suitable data.
guard let viewController = dataSource?.viewControllerAtIndex(index) else {
return nil
}
viewController.view.tag = index
return viewController
}
fileprivate func getNumberOfPages() -> Int {
if let dataSource = dataSource,
let numberOfPages = dataSource.numberOfPages()
, pageViewController != nil {
return numberOfPages
}
return 0
}
// MARK: - Public methods
open func reloadData() {
if getNumberOfPages() > 0
&& currentIndex >= 0
&& currentIndex < getNumberOfPages() {
self.pageViewController.setViewControllers(
[getViewControllerAtIndex(currentIndex)!],
direction: UIPageViewControllerNavigationDirection.forward,
animated: false,
completion: nil)
pageControl?.currentPage = currentIndex
if callFirstItemOnCreated {
delegate?.onPageSelected(getViewControllerAtIndex(currentIndex)!, index: currentIndex)
}
}
}
open func setCurrentIndex(_ index: Int, animated: Bool) {
if index == currentIndex {
print("Same page")
return
}
if index >= getNumberOfPages() {
print("Trying to reach an unknown page")
return
}
let direction: UIPageViewControllerNavigationDirection = currentIndex < index ? .forward : .reverse
guard let viewController = getViewControllerAtIndex(index) else {
print("Method getViewControllerAtIndex(\(index)) is returning nil")
return
}
self.pageViewController.setViewControllers([viewController], direction: direction, animated: true, completion: { _ in
self.currentIndex = index
})
delegate?.onPageSelected(viewController, index: index)
}
open func setScrollEnabled(_ enabled: Bool) {
if let pageViewController = pageViewController {
for view in pageViewController.view.subviews {
if let scrollView = view as? UIScrollView {
scrollView.isScrollEnabled = enabled
}
}
}
}
// MARK: - UIPageViewControllerDataSource
open func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = viewController.view.tag
if (index == 0) || (index == NSNotFound) { return nil }
index -= 1
return getViewControllerAtIndex(index)
}
open func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = viewController.view.tag
if index == NSNotFound { return nil }
index += 1
if (index == getNumberOfPages()) { return nil }
return getViewControllerAtIndex(index)
}
// MARK: - UIPageViewControllerDelegate
open func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
if let pageVC = pageViewController.viewControllers?.last {
delegate?.onPageSelected(pageVC, index: pageVC.view.tag)
// Save currentIndex
currentIndex = pageVC.view.tag
pageControl?.currentPage = currentIndex
}
}
}
// MARK: - UIScrollViewDelegate
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let direction = scrollView.contentOffset.x > frame.size.width ? 1 : -1
let point = scrollView.contentOffset
let percentComplete: CGFloat = fabs(point.x - frame.size.width)/frame.size.width
notifyAnimationDelegates(percentComplete: percentComplete, direction: direction)
}
func notifyAnimationDelegates(percentComplete: CGFloat, direction: Int) {
guard let dataSource = dataSource,
let numberOfPages = dataSource.numberOfPages() else {
return
}
for i in 0..<numberOfPages {
if let vc = dataSource.viewControllerAtIndex(i),
let animationDelegate = vc as? OkPagerViewAnimationDelegate, vc.isViewLoaded {
let index = CGFloat(i - currentIndex)
let offset: CGFloat
if index == 0 { // Current index
offset = percentComplete * CGFloat(direction)
} else {
offset = index - (percentComplete * CGFloat(direction))
}
animationDelegate.onScrolling(vc, index: i, offset: offset)
}
}
}
}
fileprivate extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder?.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}