Authored by gaoqiang xu

优化进度条

@@ -10,9 +10,9 @@ @@ -10,9 +10,9 @@
10 10
11 NSString *completeRPCURLPath = @"/yhwebviewprogressproxy/complete"; 11 NSString *completeRPCURLPath = @"/yhwebviewprogressproxy/complete";
12 12
13 -static const float YHWebViewProgressInitialValue = 0.7f;  
14 -static const float YHWebViewProgressInteractiveValue = 0.9f;  
15 -static const float YHWebViewProgressFinalProgressValue = 0.9f; 13 +static const float YHWebViewProgressInitialValue = 0.94f;
  14 +static const float YHWebViewProgressInteractiveValue = 0.95f;
  15 +static const float YHWebViewProgressFinalProgressValue = 0.96f;
16 16
17 @interface YHWebViewProgress () 17 @interface YHWebViewProgress ()
18 @property (nonatomic) NSUInteger loadingCount; 18 @property (nonatomic) NSUInteger loadingCount;
@@ -53,10 +53,15 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f; @@ -53,10 +53,15 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f;
53 { 53 {
54 float progress = self.progress; 54 float progress = self.progress;
55 float maxProgress = self.interactive?YHWebViewProgressFinalProgressValue:YHWebViewProgressInteractiveValue; 55 float maxProgress = self.interactive?YHWebViewProgressFinalProgressValue:YHWebViewProgressInteractiveValue;
  56 + if (self.loadingCount == 0) {
  57 + progress = maxProgress;
  58 + } else {
56 float remainPercent = (float)self.loadingCount/self.maxLoadCount; 59 float remainPercent = (float)self.loadingCount/self.maxLoadCount;
57 float increment = (maxProgress-progress) * remainPercent; 60 float increment = (maxProgress-progress) * remainPercent;
58 progress += increment; 61 progress += increment;
59 progress = fminf(progress, maxProgress); 62 progress = fminf(progress, maxProgress);
  63 + }
  64 +
60 [self setProgress:progress]; 65 [self setProgress:progress];
61 } 66 }
62 67
@@ -133,7 +138,7 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f; @@ -133,7 +138,7 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f;
133 138
134 self.loadingCount++; 139 self.loadingCount++;
135 140
136 - self.maxLoadCount = fmax(self.loadingCount, self.loadingCount); 141 + self.maxLoadCount = self.loadingCount;
137 142
138 [self startProgress]; 143 [self startProgress];
139 } 144 }
@@ -172,8 +177,10 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f; @@ -172,8 +177,10 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f;
172 [self.webViewProxy webView:webView didFailLoadWithError:error]; 177 [self.webViewProxy webView:webView didFailLoadWithError:error];
173 } 178 }
174 179
175 - _loadingCount--; 180 + if (error && error.code != NSURLErrorCancelled) {
  181 + self.loadingCount--;
176 [self incrementProgress]; 182 [self incrementProgress];
  183 + }
177 184
178 NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"]; 185 NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
179 186
@@ -186,7 +193,7 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f; @@ -186,7 +193,7 @@ static const float YHWebViewProgressFinalProgressValue = 0.9f;
186 193
187 BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL]; 194 BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL];
188 BOOL complete = [readyState isEqualToString:@"complete"]; 195 BOOL complete = [readyState isEqualToString:@"complete"];
189 - if ((complete && isNotRedirect) || error) { 196 + if ((complete && isNotRedirect) || (error && error.code != NSURLErrorCancelled)) {
190 [self completeProgress]; 197 [self completeProgress];
191 } 198 }
192 } 199 }
@@ -113,6 +113,7 @@ @@ -113,6 +113,7 @@
113 self.progressBarView.alpha = 1.f; 113 self.progressBarView.alpha = 1.f;
114 self.progressBarView.center = positionEnd; 114 self.progressBarView.center = positionEnd;
115 } 115 }
  116 +
116 } else { 117 } else {
117 [UIView animateWithDuration:animated?self.fadeAnimationDuration:0.f 118 [UIView animateWithDuration:animated?self.fadeAnimationDuration:0.f
118 delay:0 119 delay:0
@@ -125,14 +126,19 @@ @@ -125,14 +126,19 @@
125 CAAnimation *animationBounds = nil; 126 CAAnimation *animationBounds = nil;
126 127
127 if (progress < 1) { 128 if (progress < 1) {
  129 +
  130 + CGFloat lastProgress = _progress;
  131 +
128 if (_progress > 0.01 && [self.progressBarView.layer animationForKey:@"positionAnimation"]) { 132 if (_progress > 0.01 && [self.progressBarView.layer animationForKey:@"positionAnimation"]) {
129 positionBegin = [self.progressBarView.layer.presentationLayer position]; 133 positionBegin = [self.progressBarView.layer.presentationLayer position];
130 self.progressBarView.layer.position = positionBegin; 134 self.progressBarView.layer.position = positionBegin;
  135 + CAKeyframeAnimation *animation = (CAKeyframeAnimation*)[self.progressBarView.layer animationForKey:@"positionAnimation"];
  136 + lastProgress = positionBegin.x / ([[animation.values lastObject] CGPointValue].x - [[animation.values firstObject] CGPointValue].x);
131 [self.progressBarView.layer removeAnimationForKey:@"positionAnimation"]; 137 [self.progressBarView.layer removeAnimationForKey:@"positionAnimation"];
132 } 138 }
133 139
134 CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 140 CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
135 - keyFrameAnimation.duration = kAnimationDurationMultiplier*(progress-_progress)*10; 141 + keyFrameAnimation.duration = kAnimationDurationMultiplier*(progress-lastProgress)*10;
136 keyFrameAnimation.keyTimes = @[ @0, @.3, @1 ]; 142 keyFrameAnimation.keyTimes = @[ @0, @.3, @1 ];
137 keyFrameAnimation.values = @[ [NSValue valueWithCGPoint:positionBegin], 143 keyFrameAnimation.values = @[ [NSValue valueWithCGPoint:positionBegin],
138 [NSValue valueWithCGPoint:CGPointMake(positionBegin.x+(positionEnd.x-positionBegin.x)*0.9, positionEnd.y)], 144 [NSValue valueWithCGPoint:CGPointMake(positionBegin.x+(positionEnd.x-positionBegin.x)*0.9, positionEnd.y)],
@@ -171,6 +177,9 @@ @@ -171,6 +177,9 @@
171 177
172 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 178 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
173 { 179 {
  180 + if (!flag) {
  181 + return;
  182 + }
174 _progress = 0.f; 183 _progress = 0.f;
175 CABasicAnimation *animationOpacity = [CABasicAnimation animationWithKeyPath:@"opacity"]; 184 CABasicAnimation *animationOpacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
176 animationOpacity.fromValue = @1; 185 animationOpacity.fromValue = @1;
@@ -38,7 +38,8 @@ NSString * const YHLinkParseResult_OriginalUrlKey = @"YHLinkParseResult_Original @@ -38,7 +38,8 @@ NSString * const YHLinkParseResult_OriginalUrlKey = @"YHLinkParseResult_Original
38 if (range.location != NSNotFound) { 38 if (range.location != NSNotFound) {
39 contentString = [contentString substringFromIndex:range.location+range.length]; 39 contentString = [contentString substringFromIndex:range.location+range.length];
40 freshUrl = [string substringToIndex:range.location]; 40 freshUrl = [string substringToIndex:range.location];
41 - if ([[freshUrl substringWithRange:NSMakeRange(freshUrl.length-1, 1)] isEqualToString:@"?"]) { 41 + if (freshUrl.length > 0
  42 + && [[freshUrl substringWithRange:NSMakeRange(freshUrl.length-1, 1)] isEqualToString:@"?"]) {
42 freshUrl = [freshUrl substringToIndex:freshUrl.length-1]; 43 freshUrl = [freshUrl substringToIndex:freshUrl.length-1];
43 } 44 }
44 } else { 45 } else {
@@ -2,4 +2,30 @@ @@ -2,4 +2,30 @@
2 <Bucket 2 <Bucket
3 type = "1" 3 type = "1"
4 version = "2.0"> 4 version = "2.0">
  5 + <Breakpoints>
  6 + <BreakpointProxy
  7 + BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
  8 + <BreakpointContent
  9 + shouldBeEnabled = "Yes"
  10 + ignoreCount = "0"
  11 + continueAfterRunningActions = "Yes"
  12 + filePath = "../YHExplorer/WebProgressView/YHWebViewProgressView.m"
  13 + timestampString = "460284588.269866"
  14 + startingColumnNumber = "9223372036854775807"
  15 + endingColumnNumber = "9223372036854775807"
  16 + startingLineNumber = "91"
  17 + endingLineNumber = "91"
  18 + landmarkName = "-setProgress:animated:"
  19 + landmarkType = "5">
  20 + <Actions>
  21 + <BreakpointActionProxy
  22 + ActionExtensionID = "Xcode.BreakpointAction.DebuggerCommand">
  23 + <ActionContent
  24 + consoleCommand = "p progress">
  25 + </ActionContent>
  26 + </BreakpointActionProxy>
  27 + </Actions>
  28 + </BreakpointContent>
  29 + </BreakpointProxy>
  30 + </Breakpoints>
5 </Bucket> 31 </Bucket>
@@ -73,7 +73,7 @@ @@ -73,7 +73,7 @@
73 } else if ([title isEqualToString:@"链接跳转"]) { 73 } else if ([title isEqualToString:@"链接跳转"]) {
74 wv.url = @"http://sealedace.com/content/images/test/h5_test.html"; 74 wv.url = @"http://sealedace.com/content/images/test/h5_test.html";
75 } else { 75 } else {
76 - wv.url = @"http://yohood.test.yoho.cn/brand/index/client/1"; 76 + wv.url = @"http://m.taobao.com";
77 } 77 }
78 78
79 [self.navigationController pushViewController:wv animated:YES]; 79 [self.navigationController pushViewController:wv animated:YES];