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
//
// IBTWebProgressBar.m
// IBTWebViewController
//
// Created by Xummer on 14/12/31.
// Copyright (c) 2014年 Xummer. All rights reserved.
//
#define PROGRESS_PHASE_1 (.1f)
#define PROGRESS_PHASE_2 (.6f)
#define PROGRESS_PHASE_3 (.8f)
#define PROGRESS_PHASE_4 (.9f)
#define ANIM_DURATION_UNIT (.2f)
#define PROGRESS_FADEOUT_DELAY (.1f)
#define PROGRESS_FADE_DURATION (0.27f)
#import "IBTWebProgressBar.h"
@import QuartzCore;
@interface IBTWebProgressBar ()
@end
@implementation IBTWebProgressBar
#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
CGRect rect = frame;
rect.size.width = 0;
self = [super initWithFrame:rect];
if (!self) {
return nil;
}
self.bIsFinish = YES;
self.oriFrame = frame;
UIColor *tintColor = [UIColor colorWithRed:22.f / 255.f green:126.f / 255.f blue:251.f / 255.f alpha:1.0]; // iOS7 Safari bar color
if ([UIApplication.sharedApplication.delegate.window respondsToSelector:@selector(setTintColor:)] && UIApplication.sharedApplication.delegate.window.tintColor) {
tintColor = UIApplication.sharedApplication.delegate.window.tintColor;
}
self.backgroundColor = tintColor;
return self;
}
// 0 - 10% 3
- (CGFloat)durationOfPhase1 {
return ANIM_DURATION_UNIT * 3;
}
// 10 - 60% 2
- (CGFloat)durationOfPhase2 {
return ANIM_DURATION_UNIT * 2 * 5;
}
// 60% - 80% 3
- (CGFloat)durationOfPhase3 {
return ANIM_DURATION_UNIT * 2 * 3;
}
// 80% - 90% 5
- (CGFloat)durationOfPhase4 {
return ANIM_DURATION_UNIT * 5;
}
// 90% - 100%
- (void)progressing {
// Waiting
}
- (void)updateProgress:(CGFloat)fProgress {
fProgress = MIN(MAX(fProgress, 0), 1);
CGRect rect = self.frame;
rect.size.width = fProgress * CGRectGetWidth(self.oriFrame);
self.frame = rect;
}
#pragma mark - Actions
- (void)start {
self.alpha = 1;
self.hidden = NO;
self.bIsFinish = NO;
__weak typeof(self)weakSelf = self;
void (^animOfPhaseProcess)(BOOL) = ^(BOOL finished) {
if (!finished) {
return;
}
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.bIsFinish = YES;
[strongSelf progressing];
};
void (^animOfPhase4)(BOOL) = ^(BOOL finished) {
if (!finished) {
return;
}
[UIView animateWithDuration:[self durationOfPhase4] delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf updateProgress:PROGRESS_PHASE_4];
}
completion:animOfPhaseProcess];
};
void (^animOfPhase3)(BOOL) = ^(BOOL finished) {
if (!finished) {
return;
}
[UIView animateWithDuration:[self durationOfPhase3] delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf updateProgress:PROGRESS_PHASE_3];
}
completion:animOfPhase4];
};
void (^animOfPhase2)(BOOL) = ^(BOOL finished) {
if (!finished) {
return;
}
[UIView animateWithDuration:[self durationOfPhase2] delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf updateProgress:PROGRESS_PHASE_2];
}
completion:animOfPhase3];
};
[UIView animateWithDuration:[self durationOfPhase1] delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf updateProgress:PROGRESS_PHASE_1];
}
completion:animOfPhase2];
}
- (void)end {
if (!_bIsFinish) {
[self.layer removeAllAnimations];
}
__weak typeof(self)weakSelf = self;
void (^animOfDismiss)(BOOL) = ^(BOOL finished) {
if (!finished) {
return;
}
[UIView animateWithDuration:PROGRESS_FADE_DURATION
delay:PROGRESS_FADEOUT_DELAY
options:UIViewAnimationOptionCurveEaseOut
animations:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.alpha = 0;
}
completion:^(BOOL finished) {
if (!finished) {
return;
}
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.hidden = YES;
[strongSelf updateProgress:0];
strongSelf.bIsFinish = YES;
}];
};
[UIView animateWithDuration:ANIM_DURATION_UNIT delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf updateProgress:1];
}
completion:animOfDismiss];
}
- (void)reset {
if (!_bIsFinish) {
[self.layer removeAllAnimations];
self.bIsFinish = YES;
}
[self updateProgress:0];
}
@end