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
|
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UIScrollViewDelegate {
@IBOutlet weak var tableView: UITableView!//列表视图
@IBOutlet weak var refreshView: RereshView!//下拉刷新视图
@IBOutlet weak var refreshViewTopLayoutConstraint: NSLayoutConstraint!//下拉刷新居上的约束
var showBottomLayer:Bool = true//显示刷新视图的底部线 变宽
var hiddenBottomLayer:Bool = true//隐藏刷新视图的底部线 即变窄
var showMiddleLayer:Bool = true//显示刷新视图的中间线 变宽
var hiddenMiddleLayer:Bool = true//隐藏刷新视图的中间线 变窄
var showTopLayer:Bool = true//显示刷新头部的底部线 变宽
var hiddenTopLayer:Bool = true//隐藏刷新视图的头部线 变窄
var refreshing = false//是否在刷新中
var loadingAcitivityIndicatorView: UIActivityIndicatorView?//上拉加载指示菊花
var showFooterLoadingView:Bool = false//是否显示上拉加载视图
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
settingUI()//设置界面
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//设置界面
func settingUI(){
let screenWidth = CGRectGetWidth(UIScreen.mainScreen().bounds)
let screenHeight = CGRectGetHeight(UIScreen.mainScreen().bounds)
loadingAcitivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
loadingAcitivityIndicatorView?.hidesWhenStopped = true
loadingAcitivityIndicatorView?.frame = CGRectMake((screenWidth - 20)/2.0,screenHeight - 40, 20, 20)
self.view.addSubview(loadingAcitivityIndicatorView!)
}
/**
添加size动画
:param: layer 要添加动画的layer
:param: size 动画到的size
*/
func addSizeAnimation(layer: CALayer,size: CGSize){
var springAnimation = POPSpringAnimation(propertyNamed: kPOPLayerSize)
springAnimation.toValue = NSValue(CGSize: size);
springAnimation.springBounciness = 18
layer.pop_addAnimation(springAnimation, forKey: "layerSpringAnimation")
}
/**
添加position动画
:param: layer 要添加动画的layer
:param: position 动画到的position
*/
func addPositionAnimation(layer: CALayer,position: CGPoint){
var springAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPosition)
springAnimation.toValue = NSValue(CGPoint: position);
springAnimation.springBounciness = 18
layer.pop_addAnimation(springAnimation, forKey: "layerSpringAnimation")
}
//UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
//UIScrollViewDelegate
//将要开始减速时
func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
var contentOffsetY = scrollView.contentOffset.y
if contentOffsetY > 0{
return
}
if contentOffsetY < -52{//偏移 52 时 我们显示加载视图
refreshing = true
scrollView.contentInset = UIEdgeInsetsMake(62.0, 0.0, 0.0, 0.0)
refreshViewTopLayoutConstraint.constant = 20//刷新视图居上约束
//移除POP动画
refreshView.topLayer?.pop_removeAllAnimations()
refreshView.middleLayer?.pop_removeAllAnimations()
refreshView.bottomLayer?.pop_removeAllAnimations()
//设定layer位置
refreshView.topLayer?.frame = CGRectMake(121, 0, 4, 4)
refreshView.middleLayer?.frame = CGRectMake(98, 14, 4, 4)
refreshView.bottomLayer?.frame = CGRectMake(75, 28, 4, 4)
refreshView.textLayer?.opacity = 0
self.addPositionAnimation(self.refreshView.topLayer!,position: CGPointMake(121, 14))
self.addPositionAnimation(self.refreshView.middleLayer!,position: CGPointMake(98, 14))
self.addPositionAnimation(self.refreshView.bottomLayer!,position: CGPointMake(75, 14))
let delayTime = dispatch_time(DISPATCH_TIME_NOW,Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime,dispatch_get_main_queue()) {
UIView.animateWithDuration(0.3, animations: { () -> Void in
scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
self.refreshViewTopLayoutConstraint.constant = -52
}, completion: { (Bool) -> Void in
self.refreshing = false
self.refreshView.topLayer!.frame = CGRectMake(98, 0, 4, 4)
self.refreshView.middleLayer!.frame = CGRectMake(98, 14, 4, 4)
self.refreshView.bottomLayer!.frame = CGRectMake(98, 28, 4, 4)
})
}
}
}
//滚动时
func scrollViewDidScroll(scrollView: UIScrollView) {
var contentOffsetY = scrollView.contentOffset.y
if contentOffsetY <= 0 {
if refreshing{
return;
}
self.refreshView.hidden = false
refreshViewTopLayoutConstraint.constant = -contentOffsetY - 52
if contentOffsetY < -30{
self.hiddenBottomLayer = true
if showBottomLayer{
showBottomLayer = false
self.addSizeAnimation(refreshView.bottomLayer!, size: CGSizeMake(50, 4));
refreshView.bottomLayer?.backgroundColor = UIColor(red: 64/255.0, green: 64/255.0, blue: 64/255.0, alpha: 1.0).CGColor
}
if contentOffsetY < -46{
self.hiddenMiddleLayer = true
if showMiddleLayer{
showMiddleLayer = false
self.addSizeAnimation(refreshView.middleLayer!, size: CGSizeMake(50, 4));
refreshView.middleLayer?.backgroundColor = UIColor(red: 64/255.0, green: 64/255.0, blue: 64/255.0, alpha: 1.0).CGColor
}
if contentOffsetY < -62{
self.hiddenTopLayer = true
if showTopLayer{
showTopLayer = false
self.addSizeAnimation(refreshView.topLayer!, size: CGSizeMake(50, 4));
refreshView.topLayer?.backgroundColor = UIColor(red: 192/255.0, green: 47/255.0, blue: 46/255.0, alpha: 1.0).CGColor
refreshView.middleLayer?.backgroundColor = UIColor(red: 192/255.0, green: 47/255.0, blue: 46/255.0, alpha: 1.0).CGColor
refreshView.bottomLayer?.backgroundColor = UIColor(red: 192/255.0, green: 47/255.0, blue: 46/255.0, alpha: 1.0).CGColor
refreshView.textLayer?.foregroundColor = UIColor(red: 192/255.0, green: 47/255.0, blue: 46/255.0, alpha: 1.0).CGColor
}
}else{
self.showTopLayer = true
if hiddenTopLayer{
hiddenTopLayer = false
self.addSizeAnimation(refreshView.topLayer!, size: CGSizeMake(4, 4));
refreshView.topLayer?.backgroundColor = UIColor(red: 200/255.0, green: 200/255.0, blue: 200/255.0, alpha: 1.0).CGColor
refreshView.middleLayer?.backgroundColor = UIColor(red: 64/255.0, green: 64/255.0, blue: 64/255.0, alpha: 1.0).CGColor
refreshView.bottomLayer?.backgroundColor = UIColor(red: 64/255.0, green: 64/255.0, blue: 64/255.0, alpha: 1.0).CGColor
refreshView.textLayer?.foregroundColor = UIColor(red: 64/255.0, green: 64/255.0, blue: 64/255.0, alpha: 1.0).CGColor
}
}
}else{
self.showMiddleLayer = true
if hiddenMiddleLayer{
hiddenMiddleLayer = false
self.addSizeAnimation(refreshView.middleLayer!, size: CGSizeMake(4, 4));
refreshView.middleLayer?.backgroundColor = UIColor(red: 200/255.0, green: 200/255.0, blue: 200/255.0, alpha: 1.0).CGColor
}
}
}else{
self.showBottomLayer = true
if hiddenBottomLayer{
hiddenBottomLayer = false
self.addSizeAnimation(refreshView.bottomLayer!, size: CGSizeMake(4, 4));
refreshView.bottomLayer?.backgroundColor = UIColor(red: 200/255.0, green: 200/255.0, blue: 200/255.0, alpha: 1.0).CGColor
}
}
refreshView.textLayer?.opacity = Float(-contentOffsetY/62.0)
}else{
self.refreshView.hidden = true
let screenWidth = CGRectGetWidth(UIScreen.mainScreen().bounds)
}
if scrollView.contentOffset.y + scrollView.frame.size.height < scrollView.contentSize.height{
loadingAcitivityIndicatorView?.stopAnimating()
tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
showFooterLoadingView = false
}else{
tableView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
loadingAcitivityIndicatorView?.startAnimating()
showFooterLoadingView = true;
//添加上拉加载数据代码
//...
}
}
}
|