-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopImageStackView.swift
149 lines (115 loc) · 4.46 KB
/
TopImageStackView.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
//
// TestStackView.swift
// StretchStackViewSample
//
// Created by Hanwe LEE on 2022/06/30.
//
import UIKit
import SnapKit
import Then
import RxSwift
import RxCocoa
@objc protocol TopImageStackViewDelegate: AnyObject {
@objc optional func topImageShowPercent(_ view: TopImageStackView, percent: Float)
}
class TopImageStackView: UIView {
// MARK: private UI property
private lazy var scrollView = UIScrollView().then {
$0.backgroundColor = .clear
$0.contentInsetAdjustmentBehavior = .never
$0.delegate = self
}
private lazy var topImageView = UIImageView().then {
$0.image = self.topImage
$0.contentMode = .scaleToFill
}
private let stackView = UIStackView().then {
$0.backgroundColor = .clear
$0.axis = .vertical
}
// MARK: private property
weak var delegate: TopImageStackViewDelegate?
// MARK: property
let topImage: UIImage
let topImageHeight: CGFloat
// MARK: lifeCycle
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(topImage: UIImage, topImageHeight: CGFloat) {
self.topImage = topImage
self.topImageHeight = topImageHeight
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
setup()
}
// MARK: private func
private func setup() {
self.addSubview(self.scrollView)
self.scrollView.snp.makeConstraints {
$0.top.left.right.bottom.equalTo(self)
}
self.scrollView.addSubview(self.stackView)
self.stackView.snp.makeConstraints {
$0.top.equalTo(self.scrollView.snp.top).offset(self.topImageHeight)
$0.left.right.bottom.equalTo(self.scrollView)
$0.width.equalTo(self.snp.width)
}
self.scrollView.addSubview(topImageView)
self.topImageView.snp.makeConstraints {
$0.centerX.equalTo(self.scrollView)
$0.height.equalTo(self.topImageHeight)
$0.width.equalTo(self.scrollView)
$0.bottom.equalTo(self.stackView.snp.top)
}
}
// MARK: func
func addArrangedSubview(_ view: UIView) {
self.stackView.addArrangedSubview(view)
}
}
extension TopImageStackView: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y <= 0 {
self.delegate?.topImageShowPercent?(self, percent: 0)
} else if scrollView.contentOffset.y >= self.topImageHeight {
self.delegate?.topImageShowPercent?(self, percent: 1)
} else {
self.delegate?.topImageShowPercent?(self, percent: Float(scrollView.contentOffset.y / self.topImageHeight))
}
if scrollView.contentOffset.y < 0 {
self.topImageView.snp.updateConstraints {
$0.height.equalTo(self.topImageHeight + abs(scrollView.contentOffset.y))
$0.width.equalTo(self.scrollView).offset(abs(scrollView.contentOffset.y))
}
} else if scrollView.contentOffset.y == 0 {
self.topImageView.snp.updateConstraints {
$0.height.equalTo(self.topImageHeight)
$0.width.equalTo(self.scrollView)
}
}
}
}
class TopImageStackViewDelegateProxy: DelegateProxy<TopImageStackView, TopImageStackViewDelegate>, DelegateProxyType, TopImageStackViewDelegate {
static func registerKnownImplementations() {
self.register { (viewController) -> TopImageStackViewDelegateProxy in
TopImageStackViewDelegateProxy(parentObject: viewController, delegateProxy: self)
}
}
static func currentDelegate(for object: TopImageStackView) -> TopImageStackViewDelegate? {
return object.delegate
}
static func setCurrentDelegate(_ delegate: TopImageStackViewDelegate?, to object: TopImageStackView) {
object.delegate = delegate
}
}
extension Reactive where Base == TopImageStackView {
var delegate: DelegateProxy<TopImageStackView, TopImageStackViewDelegate> {
return TopImageStackViewDelegateProxy.proxy(for: self.base)
}
var showPercent: Observable<Float> {
return delegate.methodInvoked(#selector(TopImageStackViewDelegate.topImageShowPercent(_:percent:)))
.map { param in
return param[1] as? Float ?? 0
}
}
}