作者:遇遇yuyu | 来源:互联网 | 2024-11-18 21:13
本文探讨了在UIScrollView上嵌入Webview时遇到的一个常见问题:点击图片放大并返回后,Webview无法立即滑动。我们将分析问题原因,并提供有效的解决方案。
1: 问题描述:在类似网易新闻的详情页设计中,当在一个UIScrollView上嵌套Webview,并且Webview内包含图片和文本时,用户点击图片放大查看,然后点击返回按钮回到新闻详情页时,发现无法立即滑动整个页面。
2: 代码示例:
// 在此方法中处理Webview的触控事件,以响应长按操作
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
| - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *requestString = [[request URL] absoluteString]; NSArray *compOnents= [requestString componentsSeparatedByString:@":"]; if ([components count] > 1 && [[components objectAtIndex:0] isEqualToString:@"myweb"]) { if ([[components objectAtIndex:1] isEqualToString:@"touch"]) { [_timer invalidate]; _timer = nil; _gesState = GESTURE_STATE_END; if ([[components objectAtIndex:2] isEqualToString:@"start"]) { _gesState = GESTURE_STATE_START; float ptX = [[components objectAtIndex:3] floatValue]; float ptY = [[components objectAtIndex:4] floatValue]; NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", ptX, ptY]; NSString *tagName = [webView stringByEvaluatingJavascriptFromString:js]; if ([tagName isEqualToString:@"IMG"]) { _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleLongTouch) userInfo:nil repeats:NO]; _fangdaTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(fangdaClick) userInfo:nil repeats:NO]; } } else if ([[components objectAtIndex:2] isEqualToString:@"move"]) { _gesState = GESTURE_STATE_MOVE; } else if ([[components objectAtIndex:2] isEqualToString:@"end"]) { _gesState = GESTURE_STATE_MOVE; } } else if ([[components objectAtIndex:2] isEqualToString:@"end"]) { [_timer invalidate]; _timer = nil; _gesState = GESTURE_STATE_END; } return NO; } return YES; } |
// 当Webview加载完成后调用的方法
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
| - (void)webViewDidFinishLoad:(UIWebView *)webView { if (self.nightMode) { [webView stringByEvaluatingJavascriptFromString:@"document.getElementsByTagName('body')[0].style.color = '#000'"]; } else { [webView stringByEvaluatingJavascriptFromString:@"document.getElementsByTagName('body')[0].style.color = '#6d6d6d'"]; } [webView stringByEvaluatingJavascriptFromString:@"document.getElementsByTagName('body')[0].style.webkitOverflowScrolling = 'touch'"]; // 设置Webview的尺寸 CGRect frame = webView.frame; frame.size.height = webView.scrollView.contentSize.height; webView.frame = frame; // 处理其他UI元素的位置调整 // ... // 响应触摸事件 [webView stringByEvaluatingJavascriptFromString:kTouchJavascriptString]; } |
问题分析:当用户从图片查看器返回到详情页时,`shouldStartLoadWithRequest:` 方法会被调用两次,这导致了触控事件的响应延迟。为了解决这个问题,可以在返回时手动重置或重新初始化相关的计时器和状态变量,确保触控事件能够及时响应。