YHWebViewProgressView.m
13.1 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//
// YHWebViewProgressView.m
// YohoExplorerDemo
//
// Created by gaoqiang xu on 3/25/15.
// Copyright (c) 2015 gaoqiang xu. All rights reserved.
//
#import "YHWebViewProgressView.h"
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
//大于等于8.0的ios版本
#define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
#define kAnimationDurationMultiplier (1.8)
@interface YHWebViewProgressView ()
<CAAnimationDelegate>
@property (nonatomic) BOOL isWkWebView;
@property (nonatomic, strong)UIImageView *maskView;
@end
@implementation YHWebViewProgressView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self configureViews];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self configureViews];
}
- (void)dealloc
{
}
- (void)configureViews
{
self.isWkWebView = NO;
self.userInteractionEnabled = NO;
self.clipsToBounds = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_progressBarView = [[UIView alloc] initWithFrame:self.bounds];
_progressBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
UIColor *tintColor = [UIColor colorWithRed:88/255.f
green:88/255.f
blue:88/255.f
alpha:1.f];
if ([UIApplication.sharedApplication.delegate.window respondsToSelector:@selector(setTintColor:)]
&& UIApplication.sharedApplication.delegate.window.tintColor) {
tintColor = UIApplication.sharedApplication.delegate.window.tintColor;
}
_progressBarView.backgroundColor = tintColor;
[self addSubview:_progressBarView];
_barAnimationDuration = 0.5f;
_fadeAnimationDuration = 0.4f;
[self setProgress:0.f];
}
- (void)setProgressBarColor:(UIColor *)progressBarColor
{
_progressBarView.backgroundColor = progressBarColor;
}
- (UIColor *)progressBarColor
{
return self.progressBarView.backgroundColor;
}
- (void)setProgress:(float)progress
{
[self setProgress:progress animated:NO];
}
-(UIImageView *)maskView
{
if (!_maskView) {
_maskView = [[UIImageView alloc]init];
_maskView.image= [UIImage imageNamed:@"Mask"];
}
return _maskView;
}
- (void)useWkWebView:(WKWebView *)webView
{
if (!webView) {
return;
}
self.isWkWebView = YES;
[webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)setProgress:(float)progress animated:(BOOL)animated
{
BOOL isGrowing = progress > 0.f;
// [self.maskView setFrame:CGRectMake(0, 0, CGRectGetWidth(self.progressBarView.frame), CGRectGetHeight(self.progressBarView.frame))];
// [self.progressBarView addSubview:self.maskView];
CGFloat originX = -CGRectGetWidth(self.bounds)/2;
CGPoint positionBegin = CGPointMake(originX+_progress * self.bounds.size.width, CGRectGetHeight(self.progressBarView.frame)/2);
CGPoint positionEnd = CGPointMake(originX+progress * self.bounds.size.width, CGRectGetHeight(self.progressBarView.frame)/2);
// [self.maskView setFrame:CGRectMake(0, 0,progress * self.bounds.size.width, 2)];
if (progress < _progress) {
animated = NO;
}
if (!isGrowing) {
if (animated) {
[UIView animateWithDuration:animated?self.fadeAnimationDuration:0.f
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.progressBarView.center = positionEnd;
self.progressBarView.alpha = 1.f;
} completion:^(BOOL finished) {}];
} else {
self.progressBarView.alpha = 1.f;
self.progressBarView.center = positionEnd;
}
} else {
[UIView animateWithDuration:animated?self.fadeAnimationDuration:0.f
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.progressBarView.alpha = 1.f;
} completion:^(BOOL finished) {}];
if (animated) {
CAAnimation *animationBounds = nil;
if (progress < 1) {
CGFloat lastProgress = _progress;
if (_progress > 0.01 && [self.progressBarView.layer animationForKey:@"positionAnimation"]) {
positionBegin = [self.progressBarView.layer.presentationLayer position];
self.progressBarView.layer.position = positionBegin;
CAKeyframeAnimation *animation = (CAKeyframeAnimation*)[self.progressBarView.layer animationForKey:@"positionAnimation"];
lastProgress = positionBegin.x / ([[animation.values lastObject] CGPointValue].x - [[animation.values firstObject] CGPointValue].x);
[self.progressBarView.layer removeAnimationForKey:@"positionAnimation"];
}
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.duration = kAnimationDurationMultiplier*(progress-lastProgress)*10;
keyFrameAnimation.keyTimes = @[ @0, @.3, @1 ];
keyFrameAnimation.values = @[ [NSValue valueWithCGPoint:positionBegin],
[NSValue valueWithCGPoint:CGPointMake(positionBegin.x+(positionEnd.x-positionBegin.x)*0.9, positionEnd.y)],
[NSValue valueWithCGPoint:positionEnd] ];
keyFrameAnimation.timingFunctions = @[ [CAMediaTimingFunction functionWithControlPoints: 0.092 : 0.000 : 0.618 : 1.000],
[CAMediaTimingFunction functionWithControlPoints: 0.000 : 0.688 : 0.479 : 1.000] ];
animationBounds = keyFrameAnimation;
} else {
if (_progress > 0.05 && [self.progressBarView.layer animationForKey:@"positionAnimation"]) {
positionBegin = [self.progressBarView.layer.presentationLayer position];
self.progressBarView.layer.position = positionBegin;
[self.progressBarView.layer removeAnimationForKey:@"positionAnimation"];
}
CABasicAnimation *basicAnimationBounds = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimationBounds.fromValue = [NSValue valueWithCGPoint:positionBegin];
basicAnimationBounds.toValue = [NSValue valueWithCGPoint:positionEnd];
basicAnimationBounds.duration = self.barAnimationDuration;
basicAnimationBounds.timingFunction = [CAMediaTimingFunction functionWithControlPoints: 0.486 : 0.056 : 0.778 : 0.480];
basicAnimationBounds.delegate = self;
animationBounds = basicAnimationBounds;
}
[self.progressBarView.layer addAnimation:animationBounds forKey:@"positionAnimation"];
self.progressBarView.layer.position = positionEnd;
} else {
self.progressBarView.layer.position = positionEnd;
}
}
_progress = progress;
}
//- (void)setProgress:(float)progress animated:(BOOL)animated
//{
// BOOL isGrowing = progress > 0.f;
//
// CGFloat originX = -CGRectGetWidth(self.bounds)/2;
// CGPoint positionBegin = CGPointMake(originX+_progress * self.bounds.size.width, CGRectGetHeight(self.progressBarView.frame)/2);
// CGPoint positionEnd = CGPointMake(originX+progress * self.bounds.size.width, CGRectGetHeight(self.progressBarView.frame)/2);
//
// if (progress < _progress) {
// animated = NO;
// }
//
// if (!isGrowing) {
// if (animated) {
// [UIView animateWithDuration:animated?self.fadeAnimationDuration:0.f
// delay:0
// options:UIViewAnimationOptionCurveEaseInOut
// animations:^{
// self.progressBarView.center = positionEnd;
// self.progressBarView.alpha = 1.f;
// } completion:^(BOOL finished) {}];
// } else {
// self.progressBarView.alpha = 1.f;
// self.progressBarView.center = positionEnd;
// }
//
// } else {
// [UIView animateWithDuration:animated?self.fadeAnimationDuration:0.f
// delay:0
// options:UIViewAnimationOptionCurveEaseInOut
// animations:^{
// self.progressBarView.alpha = 1.f;
// } completion:^(BOOL finished) {}];
//
// if (animated) {
// CAAnimation *animationBounds = nil;
//
// if (progress < 1) {
//
// CGFloat lastProgress = _progress;
//
// if (_progress > 0.01 && [self.progressBarView.layer animationForKey:@"positionAnimation"]) {
// positionBegin = [self.progressBarView.layer.presentationLayer position];
// self.progressBarView.layer.position = positionBegin;
// CAKeyframeAnimation *animation = (CAKeyframeAnimation*)[self.progressBarView.layer animationForKey:@"positionAnimation"];
// lastProgress = positionBegin.x / ([[animation.values lastObject] CGPointValue].x - [[animation.values firstObject] CGPointValue].x);
// [self.progressBarView.layer removeAnimationForKey:@"positionAnimation"];
// }
//
// CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// keyFrameAnimation.duration = kAnimationDurationMultiplier*(progress-lastProgress)*10;
// keyFrameAnimation.keyTimes = @[ @0, @.3, @1 ];
// keyFrameAnimation.values = @[ [NSValue valueWithCGPoint:positionBegin],
// [NSValue valueWithCGPoint:CGPointMake(positionBegin.x+(positionEnd.x-positionBegin.x)*0.9, positionEnd.y)],
// [NSValue valueWithCGPoint:positionEnd] ];
// keyFrameAnimation.timingFunctions = @[ [CAMediaTimingFunction functionWithControlPoints: 0.092 : 0.000 : 0.618 : 1.000],
// [CAMediaTimingFunction functionWithControlPoints: 0.000 : 0.688 : 0.479 : 1.000] ];
//
// animationBounds = keyFrameAnimation;
// } else {
// if (_progress > 0.05 && [self.progressBarView.layer animationForKey:@"positionAnimation"]) {
// positionBegin = [self.progressBarView.layer.presentationLayer position];
// self.progressBarView.layer.position = positionBegin;
// [self.progressBarView.layer removeAnimationForKey:@"positionAnimation"];
// }
// CABasicAnimation *basicAnimationBounds = [CABasicAnimation animationWithKeyPath:@"position"];
// basicAnimationBounds.fromValue = [NSValue valueWithCGPoint:positionBegin];
// basicAnimationBounds.toValue = [NSValue valueWithCGPoint:positionEnd];
// basicAnimationBounds.duration = self.barAnimationDuration;
// basicAnimationBounds.timingFunction = [CAMediaTimingFunction functionWithControlPoints: 0.486 : 0.056 : 0.778 : 0.480];
//
// basicAnimationBounds.delegate = self;
//
// animationBounds = basicAnimationBounds;
// }
//
// [self.progressBarView.layer addAnimation:animationBounds forKey:@"positionAnimation"];
// self.progressBarView.layer.position = positionEnd;
//
// } else {
// self.progressBarView.layer.position = positionEnd;
// }
// }
//
// _progress = progress;
//}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
if (!flag) {
return;
}
_progress = 0.f;
CABasicAnimation *animationOpacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
animationOpacity.fromValue = @1;
animationOpacity.toValue = @0;
animationOpacity.duration = self.fadeAnimationDuration;
animationOpacity.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.progressBarView.layer addAnimation:animationOpacity forKey:@"opacityAnimation"];
self.progressBarView.layer.opacity = 0;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self setProgress:[change[@"new"] doubleValue] animated:YES];
}
@end