-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPageView.swift
More file actions
99 lines (85 loc) · 3.75 KB
/
Copy pathPageView.swift
File metadata and controls
99 lines (85 loc) · 3.75 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
import Foundation
import JamitFoundation
import UIKit
/// A stateful view which adds paged scroll behaviour to an embedded `ContentView`.
///
/// Example:
/// ```swift
/// // Represents an image view with paged scroll behaviour.
/// let imageSourceURLs: [URL] = <#image sources#>
/// let contentView = PageView<ImageView>.instantiate()
///
/// contentView.model = .init(pages: imageSourceURLs.map { .url($0) })
/// ```
public final class PageView<ContentView: StatefulViewProtocol>: StatefulView<PageViewModel<ContentView.Model>>, UIScrollViewDelegate {
private lazy var scrollView: UIScrollView = .instantiate()
private lazy var contentViews: [ContentView] = []
private var contentViewIndex: Int = 0
public override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.translatesAutoresizingMaskIntoConstraints = false
addSubview(scrollView)
scrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
public override func didChangeModel() {
super.didChangeModel()
switch model.axis {
case .horizontal:
scrollView.alwaysBounceHorizontal = true
scrollView.alwaysBounceVertical = false
case .vertical:
scrollView.alwaysBounceHorizontal = false
scrollView.alwaysBounceVertical = true
}
if model.pages.count < contentViews.count {
let removedIndices = model.pages.count ..< contentViews.count
contentViews[removedIndices].forEach { $0.removeFromSuperview() }
contentViews.removeSubrange(removedIndices)
} else if contentViews.count < model.pages.count {
let addedIndices = contentViews.count ..< model.pages.count
addedIndices.forEach { _ in
let view: ContentView = .instantiate()
contentViews.append(view)
scrollView.addSubview(view)
}
}
contentViews.enumerated().forEach { index, view in
view.model = model.pages[index]
}
updateContentLayout()
}
private func updateContentLayout() {
let totalContentDimension = CGFloat(contentViews.count) * min(bounds.width, bounds.height)
scrollView.contentSize = CGSize(
width: model.axis == .horizontal ? totalContentDimension : bounds.width,
height: model.axis == .vertical ? totalContentDimension : bounds.height
)
let initialFrame = bounds
contentViews.enumerated().forEach { index, view in
view.frame = initialFrame.offsetBy(
dx: model.axis == .horizontal ? CGFloat(index) * initialFrame.width : 0,
dy: model.axis == .vertical ? CGFloat(index) * initialFrame.height : 0
)
}
}
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let newContentViewIndex: Int
switch model.axis {
case .horizontal:
newContentViewIndex = bounds.width > 0 ? Int(round(scrollView.contentOffset.x / bounds.width)) : 0
case .vertical:
newContentViewIndex = bounds.height > 0 ? Int(round(scrollView.contentOffset.y / bounds.height)) : 0
}
if contentViewIndex != newContentViewIndex {
contentViewIndex = newContentViewIndex
model.onPageIndexChanged(contentViewIndex)
}
}
}