-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathESRefreshHeaderAnimator.swift
154 lines (137 loc) · 6.06 KB
/
ESRefreshHeaderAnimator.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
//
// ESRefreshHeaderView.swift
//
// Created by egg swift on 16/4/7.
// Copyright (c) 2013-2016 ESPullToRefresh (https://github.com/eggswift/pull-to-refresh)
// Icon from http://www.iconfont.cn
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
import QuartzCore
import UIKit
open class ESRefreshHeaderAnimator: UIView, ESRefreshProtocol, ESRefreshAnimatorProtocol, ESRefreshImpactProtocol {
open var pullToRefreshDescription = NSLocalizedString("Pull to refresh", comment: "") {
didSet {
if pullToRefreshDescription != oldValue {
titleLabel.text = pullToRefreshDescription;
}
}
}
open var releaseToRefreshDescription = NSLocalizedString("Release to refresh", comment: "")
open var loadingDescription = NSLocalizedString("Loading...", comment: "")
open var view: UIView { return self }
open var insets: UIEdgeInsets = UIEdgeInsets.zero
open var trigger: CGFloat = 60.0
open var executeIncremental: CGFloat = 60.0
open var state: ESRefreshViewState = .pullToRefresh
fileprivate let imageView: UIImageView = {
let imageView = UIImageView.init()
if /* Carthage */ let bundle = Bundle.init(identifier: "com.eggswift.ESPullToRefresh") {
imageView.image = UIImage(named: "icon_pull_to_refresh_arrow", in: bundle, compatibleWith: nil)
} else if /* CocoaPods */ let bundle = Bundle.init(identifier: "org.cocoapods.ESPullToRefresh") {
imageView.image = UIImage(named: "ESPullToRefresh.bundle/icon_pull_to_refresh_arrow", in: bundle, compatibleWith: nil)
} else /* Manual */ {
imageView.image = UIImage(named: "icon_pull_to_refresh_arrow")
}
return imageView
}()
fileprivate let titleLabel: UILabel = {
let label = UILabel.init(frame: CGRect.zero)
label.font = UIFont.systemFont(ofSize: 14.0)
label.textColor = UIColor.init(white: 0.625, alpha: 1.0)
label.textAlignment = .left
return label
}()
fileprivate let indicatorView: UIActivityIndicatorView = {
let indicatorView = UIActivityIndicatorView.init(style: .gray)
indicatorView.isHidden = true
return indicatorView
}()
public override init(frame: CGRect) {
super.init(frame: frame)
titleLabel.text = pullToRefreshDescription
self.addSubview(imageView)
self.addSubview(titleLabel)
self.addSubview(indicatorView)
}
public required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func refreshAnimationBegin(view: ESRefreshComponent) {
indicatorView.startAnimating()
indicatorView.isHidden = false
imageView.isHidden = true
titleLabel.text = loadingDescription
imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat.pi)
}
open func refreshAnimationEnd(view: ESRefreshComponent) {
indicatorView.stopAnimating()
indicatorView.isHidden = true
imageView.isHidden = false
titleLabel.text = pullToRefreshDescription
imageView.transform = CGAffineTransform.identity
}
open func refresh(view: ESRefreshComponent, progressDidChange progress: CGFloat) {
// Do nothing
}
open func refresh(view: ESRefreshComponent, stateDidChange state: ESRefreshViewState) {
guard self.state != state else {
return
}
self.state = state
switch state {
case .refreshing, .autoRefreshing:
titleLabel.text = loadingDescription
self.setNeedsLayout()
break
case .releaseToRefresh:
titleLabel.text = releaseToRefreshDescription
self.setNeedsLayout()
self.impact()
UIView.animate(withDuration: 0.2, delay: 0.0, options: UIView.AnimationOptions(), animations: {
[weak self] in
self?.imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat.pi)
}) { (animated) in }
break
case .pullToRefresh:
titleLabel.text = pullToRefreshDescription
self.setNeedsLayout()
UIView.animate(withDuration: 0.2, delay: 0.0, options: UIView.AnimationOptions(), animations: {
[weak self] in
self?.imageView.transform = CGAffineTransform.identity
}) { (animated) in }
break
default:
break
}
}
open override func layoutSubviews() {
super.layoutSubviews()
let s = self.bounds.size
let w = s.width
let h = s.height
UIView.performWithoutAnimation {
titleLabel.sizeToFit()
titleLabel.center = CGPoint.init(x: w / 2.0, y: h / 2.0)
indicatorView.center = CGPoint.init(x: titleLabel.frame.origin.x - 16.0, y: h / 2.0)
imageView.frame = CGRect.init(x: titleLabel.frame.origin.x - 28.0, y: (h - 18.0) / 2.0, width: 18.0, height: 18.0)
}
}
}