diff --git a/NJKWebViewProgress/NJKWebViewProgressView.h b/NJKWebViewProgress/NJKWebViewProgressView.h new file mode 100644 index 0000000..0f7a2ad --- /dev/null +++ b/NJKWebViewProgress/NJKWebViewProgressView.h @@ -0,0 +1,21 @@ +// +// NJKWebViewProgressView.h +// iOS 7 Style WebView Progress Bar +// +// Created by Satoshi Aasano on 11/16/13. +// Copyright (c) 2013 Satoshi Asano. All rights reserved. +// + +#import + +@interface NJKWebViewProgressView : UIView +@property (nonatomic) float progress; + +@property (nonatomic) UIView *progressBarView; +@property (nonatomic) NSTimeInterval barAnimationDuration; // default 0.1 +@property (nonatomic) NSTimeInterval fadeAnimationDuration; // default 0.27 +@property (nonatomic) NSTimeInterval fadeOutDelay; // default 0.1 + +- (void)setProgress:(float)progress animated:(BOOL)animated; + +@end diff --git a/NJKWebViewProgress/NJKWebViewProgressView.m b/NJKWebViewProgress/NJKWebViewProgressView.m new file mode 100644 index 0000000..35c5497 --- /dev/null +++ b/NJKWebViewProgress/NJKWebViewProgressView.m @@ -0,0 +1,60 @@ +// +// 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.userInteractionEnabled = NO; + self.autoresizingMask = UIViewAutoresizingFlexibleWidth; + _progressBarView = [[UIView alloc] initWithFrame:self.bounds]; + _progressBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth; + _progressBarView.backgroundColor = [UIColor colorWithRed:22.f / 255.f green:126.f / 255.f blue:251.f / 255.f alpha:1.0]; // iOS7 Safari bar color + [self addSubview:_progressBarView]; + + _barAnimationDuration = 0.27f; + _fadeAnimationDuration = 0.27f; + _fadeOutDelay = 0.1f; + } + return self; +} + +-(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