-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathNormalHeaderAnimator.swift
More file actions
executable file
·150 lines (130 loc) · 5.21 KB
/
NormalHeaderAnimator.swift
File metadata and controls
executable file
·150 lines (130 loc) · 5.21 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
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
//
// NormalHeaderAnimator.swift
// CRRefresh
//
// **************************************************
// * _____ *
// * __ _ __ ___ \ / *
// * \ \/ \/ / / __\ / / *
// * \ _ / | (__ / / *
// * \/ \/ \___/ / /__ *
// * /_____/ *
// * *
// **************************************************
// Github :https://github.com/imwcl
// HomePage:http://imwcl.com
// CSDN :http://blog.csdn.net/wang631106979
//
// Created by 王崇磊 on 16/9/14.
// Copyright © 2016年 王崇磊. All rights reserved.
//
// @class NormalFootAnimator
// @abstract 普通顶部加载Animator
// @discussion 普通顶部加载Animator
//
import UIKit
open class NormalHeaderAnimator: UIView, CRRefreshProtocol {
static let crBundle = CRRefreshBundle.bundle(name: "NormalHeader", for: NormalHeaderAnimator.self)
open var pullToRefreshDescription = crBundle?.localizedString(key: "CRRefreshHeaderIdleText") {
didSet {
if pullToRefreshDescription != oldValue {
titleLabel.text = pullToRefreshDescription;
}
}
}
open var releaseToRefreshDescription = crBundle?.localizedString(key: "CRRefreshHeaderPullingText")
open var loadingDescription = crBundle?.localizedString(key: "CRRefreshHeaderRefreshingText")
open var view: UIView { return self }
open var insets: UIEdgeInsets = .zero
open var trigger: CGFloat = 60.0
open var execute: CGFloat = 60.0
open var endDelay: CGFloat = 0
public var hold: CGFloat = 60
fileprivate let imageView: UIImageView = {
let imageView = UIImageView.init()
imageView.image = crBundle?.imageFromBundle("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 refreshBegin(view: CRRefreshComponent) {
indicatorView.startAnimating()
indicatorView.isHidden = false
imageView.isHidden = true
titleLabel.text = loadingDescription
imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat(Double.pi))
}
open func refreshEnd(view: CRRefreshComponent, finish: Bool) {
if finish {
indicatorView.stopAnimating()
indicatorView.isHidden = true
imageView.isHidden = false
imageView.transform = CGAffineTransform.identity
}else {
titleLabel.text = pullToRefreshDescription
setNeedsLayout()
}
}
public func refreshWillEnd(view: CRRefreshComponent) {
}
open func refresh(view: CRRefreshComponent, progressDidChange progress: CGFloat) {
}
open func refresh(view: CRRefreshComponent, stateDidChange state: CRRefreshState) {
switch state {
case .refreshing:
titleLabel.text = loadingDescription
setNeedsLayout()
break
case .pulling:
titleLabel.text = releaseToRefreshDescription
self.setNeedsLayout()
UIView.animate(withDuration: 0.2, delay: 0.0, options: UIView.AnimationOptions(), animations: {
[weak self] in
self?.imageView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat(Double.pi))
}) { (animated) in }
break
case .idle:
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 = bounds.size
let w = s.width
let h = s.height
UIView.performWithoutAnimation {
titleLabel.sizeToFit()
titleLabel.center = .init(x: w / 2.0, y: h / 2.0)
indicatorView.center = .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)
}
}
}