Skip to content

Commit 2714f8c

Browse files
committed
Update
1 parent 3388f20 commit 2714f8c

File tree

2 files changed

+114
-27
lines changed

2 files changed

+114
-27
lines changed

StackScrollView/Classes/ScrollableStackView.swift renamed to StackScrollView/Classes/StackScrollView.swift

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,83 @@ import EasyPeasy
2727
public class StackScrollView: UIScrollView {
2828

2929
public override init(frame: CGRect) {
30+
3031
super.init(frame: frame)
3132
setup()
3233
}
3334

3435
public required init?(coder aDecoder: NSCoder) {
36+
3537
super.init(coder: aDecoder)
3638
setup()
3739
}
3840

3941
public func setup() {
40-
addSubview(containerView)
4142

42-
containerView <- [
43+
addSubview(contentView)
44+
45+
contentView <- [
4346
Edges(),
4447
Width().like(self, .Width),
4548
]
4649
}
4750

4851
public func append(view view: UIView, animated: Bool) {
52+
4953
views.append(view)
5054
updateVerticalLayout(animated: animated)
5155
}
5256

5357
public func append(views views: [UIView], animated: Bool) {
58+
5459
self.views += views
5560
updateVerticalLayout(animated: animated)
5661
}
5762

5863
public func remove(view view: UIView, animated: Bool) {
64+
5965
if let index = views.indexOf(view) {
6066
views.removeAtIndex(index)
6167
view.removeFromSuperview()
6268
}
6369
updateVerticalLayout(animated: animated)
6470
}
6571

66-
private func updateVerticalLayout(animated animated: Bool) {
72+
public func setHidden(hidden: Bool, view: UIView, animated: Bool) {
6773

74+
func perform() {
75+
if hidden {
76+
view.superview! <- [
77+
Height(0)
78+
]
79+
} else {
80+
81+
NSLayoutConstraint.deactivateConstraints(
82+
view.superview! <- [
83+
Height(0)
84+
]
85+
)
86+
}
87+
}
6888

69-
func update() {
89+
if animated {
90+
91+
UIView.animateWithDuration(0.3, delay: 0, options: [.BeginFromCurrentState], animations: {
92+
93+
perform()
94+
self.layoutIfNeeded()
95+
96+
}) { (finish) in
97+
98+
}
99+
} else {
100+
perform()
101+
}
102+
}
103+
104+
private func updateVerticalLayout(animated animated: Bool) {
105+
106+
func perform() {
70107

71108
guard views.count > 0 else {
72109
return
@@ -75,8 +112,7 @@ public class StackScrollView: UIScrollView {
75112
if views.count == 1 {
76113

77114
let firstView = views.first!
78-
addSubViewToContainerViewIfNeeded(firstView)
79-
firstView <- [
115+
addSubViewToContainerViewIfNeeded(firstView) <- [
80116
Edges(),
81117
]
82118

@@ -87,22 +123,23 @@ public class StackScrollView: UIScrollView {
87123
let firstView = views.first!
88124
let lastView = views.last!
89125

90-
addSubViewToContainerViewIfNeeded(firstView)
91-
addSubViewToContainerViewIfNeeded(lastView)
126+
let lastContainerView = addSubViewToContainerViewIfNeeded(lastView)
127+
let firstContainerView = addSubViewToContainerViewIfNeeded(firstView)
92128

93-
firstView <- [
129+
firstContainerView <- [
94130
Top(),
95131
Right(),
96132
Left(),
97-
Bottom().to(lastView, .Top),
133+
Bottom().to(lastContainerView, .Top),
98134
]
99135

100-
lastView <- [
136+
lastContainerView <- [
101137
Right(),
102138
Left(),
103139
Bottom(),
104140
]
105141

142+
106143
return
107144
}
108145

@@ -113,31 +150,31 @@ public class StackScrollView: UIScrollView {
113150
let firstView = _views.removeFirst()
114151
let lastView = _views.removeLast()
115152

116-
addSubViewToContainerViewIfNeeded(firstView)
117-
addSubViewToContainerViewIfNeeded(lastView)
153+
let firstContainerView = addSubViewToContainerViewIfNeeded(firstView)
154+
let lastContainerView = addSubViewToContainerViewIfNeeded(lastView)
118155

119-
firstView <- [
156+
firstContainerView <- [
120157
Top(),
121158
Right(),
122159
Left(),
123160
]
124161

125-
var _topView: UIView = firstView
162+
var _topContainerView: UIView = firstView
126163

127164
for view in _views {
128165

129-
addSubViewToContainerViewIfNeeded(view)
166+
let containerView = addSubViewToContainerViewIfNeeded(view)
130167

131-
view <- [
132-
Top().to(_topView, .Bottom),
168+
containerView <- [
169+
Top().to(_topContainerView, .Bottom),
133170
Right(),
134171
Left(),
135172
]
136-
_topView = view
173+
_topContainerView = containerView
137174
}
138175

139-
lastView <- [
140-
Top().to(_topView, .Bottom),
176+
lastContainerView <- [
177+
Top().to(_topContainerView, .Bottom),
141178
Right(),
142179
Left(),
143180
Bottom(),
@@ -146,31 +183,40 @@ public class StackScrollView: UIScrollView {
146183
}
147184

148185
views.forEach {
149-
$0.frame.size.width = containerView.bounds.width
186+
$0.frame.size.width = contentView.bounds.width
150187
}
151188

152189
if animated {
153190

154191
UIView.animateWithDuration(0.3, delay: 0, options: [.BeginFromCurrentState], animations: {
155192

156-
update()
193+
perform()
157194
self.layoutIfNeeded()
158195

159196
}) { (finish) in
160197

161198
}
162199
} else {
163-
update()
200+
perform()
164201
}
165202
}
166203

167204
private var views: [UIView] = []
168205

169-
private let containerView = UIView()
206+
private let contentView = UIView()
170207

171-
private func addSubViewToContainerViewIfNeeded(view: UIView) {
172-
if view.superview != containerView {
208+
private func addSubViewToContainerViewIfNeeded(view: UIView) -> UIView {
209+
if view.superview == nil {
210+
let containerView = UIView()
211+
containerView.opaque = true
212+
containerView.backgroundColor = UIColor.clearColor()
213+
containerView.clipsToBounds = true
173214
containerView.addSubview(view)
215+
view <- [
216+
Edges().with(.MediumPriority)
217+
]
218+
contentView.addSubview(containerView)
174219
}
220+
return view.superview!
175221
}
176222
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// StackScrollViewCellType.swift
2+
//
3+
// Copyright (c) 2016 muukii
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
// THE SOFTWARE.
22+
23+
import UIKit
24+
25+
public protocol StackScrollViewCellType: class {
26+
27+
}
28+
29+
extension StackScrollViewCellType where Self: UIView {
30+
31+
public var stackScrollView: StackScrollView {
32+
let superview = self.superview?.superview?.superview
33+
precondition(superview is StackScrollView, "Must be added StackScrollView")
34+
return superview as! StackScrollView
35+
}
36+
37+
public func setHidden(hidden: Bool, animated: Bool) {
38+
39+
stackScrollView.setHidden(hidden, view: self, animated: animated)
40+
}
41+
}

0 commit comments

Comments
 (0)