ios – UIScrollView中的UIWebView

前端之家收集整理的这篇文章主要介绍了ios – UIScrollView中的UIWebView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序在UIScrollView中有几个UIWebView视图.有时我有html内容所以我用UIWebView load HTMLString:baseURL:方法加载UIWebView.这似乎不会导致问题.最近我开始使用loadRequest:方法使用url加载一些UIWebView实例.一旦我开始这样做,每次滚动时,我都会收到以下错误

*由于未捕获的异常’CALayerInvalidGeometry’而终止应用程序,原因:’CALayer位置包含NaN:[nan nan]’

使用以下堆栈跟踪:

  1. Thread 1,Queue : (null)
  2. #0 0x01dd5caa in objc_exception_throw ()
  3. #1 0x01f5da48 in +[NSException raise:format:arguments:] ()
  4. #2 0x01f5d9b9 in +[NSException raise:format:] ()
  5. #3 0x007e8c0d in CA::Layer::set_position(CA::Vec2<double> const&,bool) ()
  6. #4 0x007def55 in -[CALayer setPosition:] ()
  7. #5 0x037b07d3 in -[WebFixedPositionContent scrollOrZoomChanged:] ()
  8. #6 0x00a5bfae in -[UIWebDocumentView _updateFixedPositionContent] ()
  9. #7 0x00c66ff3 in -[UIWebBrowserView _updateFixedPositionContent] ()
  10. #8 0x00a5b07e in -[UIWebDocumentView _didScroll] ()
  11. #9 0x01fb6dea in -[NSObject performSelector:] ()
  12. #10 0x01f207f1 in -[NSArray makeObjectsPerformSelector:] ()
  13. #11 0x0091627d in -[UIScrollView(Static) _notifyDidScroll] ()
  14. #12 0x009029ae in -[UIScrollView setContentOffset:] ()
  15. #13 0x00909ac8 in -[UIScrollView _updatePanGesture] ()
  16. #14 0x0090d3c0 in -[UIScrollView handlePan:] ()
  17. #15 0x00b84e29 in _UIGestureRecognizerSendActions ()
  18. #16 0x00b84133 in -[UIGestureRecognizer _updateGestureWithEvent:] ()
  19. #17 0x00b853bf in -[UIGestureRecognizer _delayedUpdateGesture] ()
  20. #18 0x00b87a21 in ___UIGestureRecognizerUpdate_block_invoke_0541 ()
  21. #19 0x00b8797c in _UIGestureRecognizerApplyBlocksToArray ()
  22. #20 0x00b803d7 in _UIGestureRecognizerUpdate ()
  23. #21 0x008e51a2 in -[UIWindow _sendGesturesForEvent:] ()
  24. #22 0x008e5532 in -[UIWindow sendEvent:] ()

我已经看过很多有关这方面的问题和答案,并意识到苹果不鼓励这样做,但我没有看到任何提供替代方案的答案.

我的第一个问题:是否有另一种方法可以达到同样的效果

下面是我如何创建UIWebView的片段

  1. - (void) createWebView:(NSString*)url{
  2. NSInteger requestedHeight = 480;
  3. NSInteger requestedWidth = 640;
  4.  
  5. self.webContent = [[UIWebView alloc] initWithFrame: CGRectMake(0,self.frame.size.height,requestedWidth,requestedHeight)];
  6. self.webContent.scrollView.userInteractionEnabled = NO;
  7. self.webContent.scrollView.scrollEnabled = NO;
  8. self.webContent.scrollView.bounces = NO;
  9.  
  10. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  11. [self.webContent loadRequest:urlRequest];
  12.  
  13. [self addSubview:self.webContent];
  14.  
  15. NSInteger calculatedHeight = self.webContent.frame.origin.y + self.webContent.frame.size.height;
  16. NSInteger calculatedWidth = self.webContent.frame.origin.x + self.webContent.frame.size.width;
  17. if (calculatedHeight > self.frame.size.height || calculatedWidth > self.frame.size.width){
  18. self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,MAX(self.frame.size.width,calculatedWidth),MAX(self.frame.size.height,calculatedHeight + 5));
  19. }
  20. }

如您所见,我禁用了UIWebView的滚动和用户输入.

我的第二个问题:当我明确禁用该功能时,为什么UIWebView(或至少一个或多个子视图)处理滚动?

我的第三个问题:因为它似乎正在处理滚动,是否有任何其他方法拦截或禁用发送到UIWebView的滚动通知

如您所见,我并不打算让用户与UIWebView的内容进行交互 – 只是让他们看到内容.

在这种情况下,可能的替代方案是以某种方式在UIScrollView之外创建一个用户不可见的UIWebView,将其内容捕获到图像中,然后在UIImageView中显示内容?如果是这样,我如何将UIWebView的内容捕获到图像中?

编辑:
我找到了一个非常简单的解决方案.基本上加载UIWebView但不要将其添加为子视图.当UIWebView完成加载时,使用以下代码创建图像:

  1. - (void)webViewDidFinishLoad:(UIWebView *)webView{
  2. UIGraphicsBeginImageContext(self.webContent.bounds.size);
  3. [self.webContent.layer renderInContext:UIGraphicsGetCurrentContext()];
  4. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  5. UIGraphicsEndImageContext();
  6.  
  7. [self.imageContent setImage:image];
  8. }

我仍然有兴趣听取问题1到3的答案.将来,如果我们决定支持交互式UIWebView,这个解决方案将无效.

有什么建议?

解决方法

您是否已经在iOS模拟器或实际设备上测试此代码?我最近在模拟器中测试iOS 5.1时遇到了相同的崩溃,但在运行5.1.iOS 6的iPad / iPhone上没有遇到同样的崩溃.在设备和模拟器上.我相信这是由于iOS 6中的UIWebView更改,UIWebView已被更改为现在异步绘制其内容.

猜你在找的iOS相关文章