-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathNJKWebViewProgressView.m
75 lines (65 loc) · 2.46 KB
/
NJKWebViewProgressView.m
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
//
// NJKWebViewProgressView.m
//
// Created by Satoshi Aasanoon 11/16/13.
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
//
#import "NJKWebViewProgressView.h"
@implementation NJKWebViewProgressView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self configureViews];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self configureViews];
}
-(void)configureViews
{
self.userInteractionEnabled = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_progressBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGRectGetHeight(self.bounds))];
_progressBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
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;
}
_progressBarView.backgroundColor = tintColor;
[self addSubview:_progressBarView];
_barAnimationDuration = 0.27f;
_fadeAnimationDuration = 0.27f;
_fadeOutDelay = 0.1f;
}
-(void)setProgress:(float)progress
{
[self setProgress:progress animated:NO];
}
- (void)setProgress:(float)progress animated:(BOOL)animated
{
BOOL isGrowing = progress > 0.0;
[UIView animateWithDuration:(isGrowing && animated) ? _barAnimationDuration : 0.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = _progressBarView.frame;
frame.size.width = progress * self.bounds.size.width;
_progressBarView.frame = frame;
} completion:nil];
if (progress >= 1.0) {
[UIView animateWithDuration:animated ? _fadeAnimationDuration : 0.0 delay:_fadeOutDelay options:UIViewAnimationOptionCurveEaseInOut animations:^{
_progressBarView.alpha = 0.0;
} completion:^(BOOL completed){
CGRect frame = _progressBarView.frame;
frame.size.width = 0;
_progressBarView.frame = frame;
}];
}
else {
[UIView animateWithDuration:animated ? _fadeAnimationDuration : 0.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_progressBarView.alpha = 1.0;
} completion:nil];
}
}
@end