mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
#1270 (replacing UIWebView with WKWebView)
- Replaced NJKWebViewProgress with a WKWebView-compatible progress bar in the Original view.
This commit is contained in:
parent
4fad1626bc
commit
130bed8923
7 changed files with 14 additions and 388 deletions
|
@ -8,9 +8,6 @@
|
|||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "BaseViewController.h"
|
||||
//#import "SloppySwiper.h"
|
||||
#import "NJKWebViewProgressView.h"
|
||||
#import "NJKWebViewProgress.h"
|
||||
#import <WebKit/WebKit.h>
|
||||
|
||||
@class NewsBlurAppDelegate;
|
||||
|
@ -26,14 +23,14 @@ UIGestureRecognizerDelegate> {
|
|||
UIBarButtonItem *backBarButton;
|
||||
UILabel *titleView;
|
||||
UIBarButtonItem *closeButton;
|
||||
NJKWebViewProgressView *progressView;
|
||||
UIProgressView *progressView;
|
||||
BOOL finishedLoading;
|
||||
}
|
||||
|
||||
@property (nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate;
|
||||
@property (nonatomic) IBOutlet WKWebView *webView;
|
||||
//@property (strong, nonatomic) SloppySwiper *swiper;
|
||||
@property (nonatomic) NJKWebViewProgressView *progressView;
|
||||
@property (nonatomic) UIProgressView *progressView;
|
||||
@property (nonatomic, strong) NSString *customPageTitle;
|
||||
|
||||
- (void)resetProgressBar;
|
||||
|
|
|
@ -56,11 +56,13 @@
|
|||
[self.view addSubview:webView];
|
||||
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
|
||||
|
||||
CGFloat progressBarHeight = 2.f;
|
||||
CGRect navigaitonBarBounds = self.navigationController.navigationBar.bounds;
|
||||
CGRect barFrame = CGRectMake(0, navigaitonBarBounds.origin.y + navigaitonBarBounds.size.height - progressBarHeight, navigaitonBarBounds.size.width, progressBarHeight);
|
||||
progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
|
||||
CGFloat progressBarHeight = 1.f;
|
||||
CGRect navigationBarFrame = self.navigationController.navigationBar.bounds;
|
||||
CGRect barFrame = CGRectMake(0, CGRectGetMaxY(navigationBarFrame) - progressBarHeight - 2, navigationBarFrame.size.width, progressBarHeight);
|
||||
progressView = [[UIProgressView alloc] initWithFrame:barFrame];
|
||||
progressView.progressViewStyle = UIProgressViewStyleBar;
|
||||
progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
|
||||
[self.navigationController.navigationBar addSubview:progressView];
|
||||
|
||||
[[ThemeManager themeManager] addThemeGestureRecognizerToView:self.webView];
|
||||
|
||||
|
@ -92,22 +94,11 @@
|
|||
- (void)viewWillAppear:(BOOL)animated {
|
||||
[super viewWillAppear:animated];
|
||||
appDelegate.originalStoryViewNavController.navigationBar.hidden = YES;
|
||||
// self.swiper = [[SloppySwiper alloc] initWithNavigationController:self.navigationController];
|
||||
// self.navigationController.delegate = self.swiper;
|
||||
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated {
|
||||
[super viewDidAppear:animated];
|
||||
|
||||
[self.navigationController.navigationBar addSubview:progressView];
|
||||
[self resetProgressBar];
|
||||
}
|
||||
|
||||
- (void)viewWillDisappear:(BOOL)animated {
|
||||
[super viewWillDisappear:animated];
|
||||
self.navigationController.navigationBar.alpha = 1;
|
||||
[progressView removeFromSuperview];
|
||||
}
|
||||
|
||||
- (void)viewDidDisappear:(BOOL)animated {
|
||||
|
@ -132,24 +123,18 @@
|
|||
}
|
||||
|
||||
- (void)resetProgressBar {
|
||||
if (finishedLoading) return;
|
||||
|
||||
progressView.progressBarView.alpha = 0.0f;
|
||||
progressView.alpha = 0.0f;
|
||||
[progressView setProgress:0 animated:NO];
|
||||
[progressView setProgress:NJKInitialProgressValue animated:YES];
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
||||
if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webView) {
|
||||
progressView.alpha = 1.0f;
|
||||
[progressView setProgress:webView.estimatedProgress animated:YES];
|
||||
|
||||
if (webView.estimatedProgress == NJKInteractiveProgressValue) {
|
||||
// The web view has finished parsing the document,
|
||||
// but is still loading sub-resources
|
||||
}
|
||||
|
||||
if (webView.estimatedProgress == NJKFinalProgressValue) {
|
||||
if (webView.estimatedProgress >= 100) {
|
||||
finishedLoading = YES;
|
||||
[self resetProgressBar];
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -347,6 +332,7 @@
|
|||
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
||||
[self updateTitle:self.webView];
|
||||
finishedLoading = YES;
|
||||
[self resetProgressBar];
|
||||
}
|
||||
|
||||
- (void)webView:(WKWebView *)webView didFailLoadWithError:(NSError *)error
|
||||
|
@ -360,6 +346,7 @@
|
|||
[self informError:error];
|
||||
}
|
||||
finishedLoading = YES;
|
||||
[self resetProgressBar];
|
||||
}
|
||||
|
||||
# pragma mark -
|
||||
|
|
|
@ -276,8 +276,6 @@
|
|||
FF03B00119F881380063002A /* ARChromeActivity@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF03AFFA19F881380063002A /* ARChromeActivity@3x.png */; };
|
||||
FF03B00219F881380063002A /* ARChromeActivity@3x~ipad.png in Resources */ = {isa = PBXBuildFile; fileRef = FF03AFFB19F881380063002A /* ARChromeActivity@3x~ipad.png */; };
|
||||
FF03B00319F881380063002A /* ARChromeActivity~ipad.png in Resources */ = {isa = PBXBuildFile; fileRef = FF03AFFC19F881380063002A /* ARChromeActivity~ipad.png */; };
|
||||
FF03B00919F987E00063002A /* NJKWebViewProgress.m in Sources */ = {isa = PBXBuildFile; fileRef = FF03B00619F987E00063002A /* NJKWebViewProgress.m */; };
|
||||
FF03B00A19F987E00063002A /* NJKWebViewProgressView.m in Sources */ = {isa = PBXBuildFile; fileRef = FF03B00819F987E00063002A /* NJKWebViewProgressView.m */; };
|
||||
FF0FAEB117B084D2008651F9 /* OfflineCleanImages.m in Sources */ = {isa = PBXBuildFile; fileRef = FF0FAEB017B0846C008651F9 /* OfflineCleanImages.m */; };
|
||||
FF11045F176950F900502C29 /* NBLoadingCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF11045E176950F900502C29 /* NBLoadingCell.m */; };
|
||||
FF1104611769695A00502C29 /* g_icn_offline@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF1104601769695A00502C29 /* g_icn_offline@2x.png */; };
|
||||
|
@ -994,10 +992,6 @@
|
|||
FF03AFFA19F881380063002A /* ARChromeActivity@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ARChromeActivity@3x.png"; sourceTree = "<group>"; };
|
||||
FF03AFFB19F881380063002A /* ARChromeActivity@3x~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ARChromeActivity@3x~ipad.png"; sourceTree = "<group>"; };
|
||||
FF03AFFC19F881380063002A /* ARChromeActivity~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ARChromeActivity~ipad.png"; sourceTree = "<group>"; };
|
||||
FF03B00519F987E00063002A /* NJKWebViewProgress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NJKWebViewProgress.h; sourceTree = "<group>"; };
|
||||
FF03B00619F987E00063002A /* NJKWebViewProgress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NJKWebViewProgress.m; sourceTree = "<group>"; };
|
||||
FF03B00719F987E00063002A /* NJKWebViewProgressView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NJKWebViewProgressView.h; sourceTree = "<group>"; };
|
||||
FF03B00819F987E00063002A /* NJKWebViewProgressView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NJKWebViewProgressView.m; sourceTree = "<group>"; };
|
||||
FF0FAEAF17B0846C008651F9 /* OfflineCleanImages.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OfflineCleanImages.h; path = offline/OfflineCleanImages.h; sourceTree = "<group>"; };
|
||||
FF0FAEB017B0846C008651F9 /* OfflineCleanImages.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = OfflineCleanImages.m; path = offline/OfflineCleanImages.m; sourceTree = "<group>"; };
|
||||
FF11045D176950F900502C29 /* NBLoadingCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBLoadingCell.h; sourceTree = "<group>"; };
|
||||
|
@ -1674,7 +1668,6 @@
|
|||
children = (
|
||||
FFC579831FB67752004677F1 /* SAConfettiView */,
|
||||
010EDEF41B238635003B79DE /* OnePasswordExtension */,
|
||||
FF03B00419F987E00063002A /* NJKWebViewProgress */,
|
||||
FF03AFF419F881380063002A /* ARChromeActivity */,
|
||||
FF03AFE619F87F2E0063002A /* TUSafariActivity */,
|
||||
FFA045AD19CA49D700618DC4 /* SloppySwiper */,
|
||||
|
@ -2342,18 +2335,6 @@
|
|||
path = "Other Sources/ARChromeActivity";
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
FF03B00419F987E00063002A /* NJKWebViewProgress */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
FF03B00519F987E00063002A /* NJKWebViewProgress.h */,
|
||||
FF03B00619F987E00063002A /* NJKWebViewProgress.m */,
|
||||
FF03B00719F987E00063002A /* NJKWebViewProgressView.h */,
|
||||
FF03B00819F987E00063002A /* NJKWebViewProgressView.m */,
|
||||
);
|
||||
name = NJKWebViewProgress;
|
||||
path = "Other Sources/NJKWebViewProgress";
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
FF22FE5216E52BAD0046165A /* Foundation */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
|
@ -3461,7 +3442,6 @@
|
|||
43F6A79D15B0CDC60092EE91 /* ActivityCell.m in Sources */,
|
||||
FF3FA88D1BB2677C001F7C32 /* NBCopyLinkActivity.m in Sources */,
|
||||
17432C831C53438D003F8FD6 /* FeedChooserViewController.m in Sources */,
|
||||
FF03B00919F987E00063002A /* NJKWebViewProgress.m in Sources */,
|
||||
010EDEFA1B2386B7003B79DE /* OnePasswordExtension.m in Sources */,
|
||||
43ABBCAA15B53A1400EA3111 /* InteractionCell.m in Sources */,
|
||||
FF34FD611E9D93CB0062F8ED /* IASKAppSettingsWebViewController.m in Sources */,
|
||||
|
@ -3518,7 +3498,6 @@
|
|||
FF6618C8176184560039913B /* NBNotifier.m in Sources */,
|
||||
FF03AFF319F87F2E0063002A /* TUSafariActivity.m in Sources */,
|
||||
FF83FF051FB52565008DAC0F /* PremiumViewController.m in Sources */,
|
||||
FF03B00A19F987E00063002A /* NJKWebViewProgressView.m in Sources */,
|
||||
FF11045F176950F900502C29 /* NBLoadingCell.m in Sources */,
|
||||
FF34FD701E9D93CB0062F8ED /* IASKTextViewCell.m in Sources */,
|
||||
FFAD89C218AC45A100D68567 /* StoriesCollection.m in Sources */,
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
//
|
||||
// NJKWebViewProgress.h
|
||||
//
|
||||
// Created by Satoshi Aasano on 4/20/13.
|
||||
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#undef njk_weak
|
||||
#if __has_feature(objc_arc_weak)
|
||||
#define njk_weak weak
|
||||
#else
|
||||
#define njk_weak unsafe_unretained
|
||||
#endif
|
||||
|
||||
extern const float NJKInitialProgressValue;
|
||||
extern const float NJKInteractiveProgressValue;
|
||||
extern const float NJKFinalProgressValue;
|
||||
|
||||
typedef void (^NJKWebViewProgressBlock)(float progress);
|
||||
@protocol NJKWebViewProgressDelegate;
|
||||
@interface NJKWebViewProgress : NSObject<UIWebViewDelegate>
|
||||
@property (nonatomic, njk_weak) id<NJKWebViewProgressDelegate>progressDelegate;
|
||||
@property (nonatomic, njk_weak) id<UIWebViewDelegate>webViewProxyDelegate;
|
||||
@property (nonatomic, copy) NJKWebViewProgressBlock progressBlock;
|
||||
@property (nonatomic, readonly) float progress; // 0.0..1.0
|
||||
|
||||
- (void)reset;
|
||||
@end
|
||||
|
||||
@protocol NJKWebViewProgressDelegate <NSObject>
|
||||
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress;
|
||||
@end
|
||||
|
|
@ -1,204 +0,0 @@
|
|||
//
|
||||
// NJKWebViewProgress.m
|
||||
//
|
||||
// Created by Satoshi Aasano on 4/20/13.
|
||||
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NJKWebViewProgress.h"
|
||||
|
||||
NSString *completeRPCURLPath = @"/njkwebviewprogressproxy/complete";
|
||||
|
||||
const float NJKInitialProgressValue = 0.1f;
|
||||
const float NJKInteractiveProgressValue = 0.5f;
|
||||
const float NJKFinalProgressValue = 0.9f;
|
||||
|
||||
@implementation NJKWebViewProgress
|
||||
{
|
||||
NSUInteger _loadingCount;
|
||||
NSUInteger _maxLoadCount;
|
||||
NSURL *_currentURL;
|
||||
BOOL _interactive;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_maxLoadCount = _loadingCount = 0;
|
||||
_interactive = NO;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)startProgress
|
||||
{
|
||||
if (_progress < NJKInitialProgressValue) {
|
||||
[self setProgress:NJKInitialProgressValue];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)incrementProgress
|
||||
{
|
||||
float progress = self.progress;
|
||||
float maxProgress = _interactive ? NJKFinalProgressValue : NJKInteractiveProgressValue;
|
||||
float remainPercent = (float)_loadingCount / (float)_maxLoadCount;
|
||||
float increment = (maxProgress - progress) * remainPercent;
|
||||
progress += increment;
|
||||
progress = fmin(progress, maxProgress);
|
||||
[self setProgress:progress];
|
||||
}
|
||||
|
||||
- (void)completeProgress
|
||||
{
|
||||
[self setProgress:1.0];
|
||||
}
|
||||
|
||||
- (void)setProgress:(float)progress
|
||||
{
|
||||
// progress should be incremental only
|
||||
if (progress > _progress || progress == 0) {
|
||||
_progress = progress;
|
||||
if ([_progressDelegate respondsToSelector:@selector(webViewProgress:updateProgress:)]) {
|
||||
[_progressDelegate webViewProgress:self updateProgress:progress];
|
||||
}
|
||||
if (_progressBlock) {
|
||||
_progressBlock(progress);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)reset
|
||||
{
|
||||
_maxLoadCount = _loadingCount = 0;
|
||||
_interactive = NO;
|
||||
[self setProgress:0.0];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark UIWebViewDelegate
|
||||
|
||||
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
|
||||
{
|
||||
if ([request.URL.path isEqualToString:completeRPCURLPath]) {
|
||||
[self completeProgress];
|
||||
return NO;
|
||||
}
|
||||
|
||||
BOOL ret = YES;
|
||||
if ([_webViewProxyDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
|
||||
ret = [_webViewProxyDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
|
||||
}
|
||||
|
||||
BOOL isFragmentJump = NO;
|
||||
if (request.URL.fragment) {
|
||||
NSString *nonFragmentURL = [request.URL.absoluteString stringByReplacingOccurrencesOfString:[@"#" stringByAppendingString:request.URL.fragment] withString:@""];
|
||||
isFragmentJump = [nonFragmentURL isEqualToString:webView.request.URL.absoluteString];
|
||||
}
|
||||
|
||||
BOOL isTopLevelNavigation = [request.mainDocumentURL isEqual:request.URL];
|
||||
|
||||
BOOL isHTTP = [request.URL.scheme isEqualToString:@"http"] || [request.URL.scheme isEqualToString:@"https"];
|
||||
if (ret && !isFragmentJump && isHTTP && isTopLevelNavigation) {
|
||||
_currentURL = request.URL;
|
||||
[self reset];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (void)webViewDidStartLoad:(UIWebView *)webView
|
||||
{
|
||||
if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
|
||||
[_webViewProxyDelegate webViewDidStartLoad:webView];
|
||||
}
|
||||
|
||||
_loadingCount++;
|
||||
_maxLoadCount = fmax(_maxLoadCount, _loadingCount);
|
||||
|
||||
[self startProgress];
|
||||
}
|
||||
|
||||
- (void)webViewDidFinishLoad:(UIWebView *)webView
|
||||
{
|
||||
if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
|
||||
[_webViewProxyDelegate webViewDidFinishLoad:webView];
|
||||
}
|
||||
|
||||
_loadingCount--;
|
||||
[self incrementProgress];
|
||||
|
||||
NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
|
||||
|
||||
BOOL interactive = [readyState isEqualToString:@"interactive"];
|
||||
if (interactive) {
|
||||
_interactive = YES;
|
||||
NSString *waitForCompleteJS = [NSString stringWithFormat:@"window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@://%@%@'; document.body.appendChild(iframe); }, false);", webView.request.mainDocumentURL.scheme, webView.request.mainDocumentURL.host, completeRPCURLPath];
|
||||
[webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS];
|
||||
}
|
||||
|
||||
BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL];
|
||||
BOOL complete = [readyState isEqualToString:@"complete"];
|
||||
if (complete && isNotRedirect) {
|
||||
[self completeProgress];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
|
||||
{
|
||||
if ([_webViewProxyDelegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
|
||||
[_webViewProxyDelegate webView:webView didFailLoadWithError:error];
|
||||
}
|
||||
|
||||
_loadingCount--;
|
||||
[self incrementProgress];
|
||||
|
||||
NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
|
||||
|
||||
BOOL interactive = [readyState isEqualToString:@"interactive"];
|
||||
if (interactive) {
|
||||
_interactive = YES;
|
||||
NSString *waitForCompleteJS = [NSString stringWithFormat:@"window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@://%@%@'; document.body.appendChild(iframe); }, false);", webView.request.mainDocumentURL.scheme, webView.request.mainDocumentURL.host, completeRPCURLPath];
|
||||
[webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS];
|
||||
}
|
||||
|
||||
BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL];
|
||||
BOOL complete = [readyState isEqualToString:@"complete"];
|
||||
if (complete && isNotRedirect) {
|
||||
[self completeProgress];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Method Forwarding
|
||||
// for future UIWebViewDelegate impl
|
||||
|
||||
- (BOOL)respondsToSelector:(SEL)aSelector
|
||||
{
|
||||
if ( [super respondsToSelector:aSelector] )
|
||||
return YES;
|
||||
|
||||
if ([self.webViewProxyDelegate respondsToSelector:aSelector])
|
||||
return YES;
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
|
||||
{
|
||||
NSMethodSignature *signature = [super methodSignatureForSelector:selector];
|
||||
if(!signature) {
|
||||
if([_webViewProxyDelegate respondsToSelector:selector]) {
|
||||
return [(NSObject *)_webViewProxyDelegate methodSignatureForSelector:selector];
|
||||
}
|
||||
}
|
||||
return signature;
|
||||
}
|
||||
|
||||
- (void)forwardInvocation:(NSInvocation*)invocation
|
||||
{
|
||||
if ([_webViewProxyDelegate respondsToSelector:[invocation selector]]) {
|
||||
[invocation invokeWithTarget:_webViewProxyDelegate];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
|
@ -1,21 +0,0 @@
|
|||
//
|
||||
// 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 <UIKit/UIKit.h>
|
||||
|
||||
@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
|
|
@ -1,77 +0,0 @@
|
|||
//
|
||||
// NJKWebViewProgressView.m
|
||||
//
|
||||
// Created by Satoshi Aasanoon 11/16/13.
|
||||
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NJKWebViewProgressView.h"
|
||||
|
||||
@implementation NJKWebViewProgressView
|
||||
|
||||
@dynamic progress;
|
||||
|
||||
- (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:self.bounds];
|
||||
_progressBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
|
||||
UIColor *tintColor = UIColorFromRGB(0x8F918B);
|
||||
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
|
Loading…
Add table
Reference in a new issue