滚动到视图中时,菜单的屏幕外文本会出现延迟 - WebView 问题? (iOS)

发布于 2024-11-29 08:32:42 字数 274 浏览 1 评论 0原文

我的 iOS 应用程序中有一个很大的向下钻取菜单。 菜单内容源自XML。 它存储在 SQLite 数据库中,并且正在使用 Webview。 为什么要使用Webview?因为文本有样式(字体大小/颜色)。

问题是这样的:当你向下滚动菜单时,之前在屏幕外的文本栏在 1/2 秒内保持不可见。他们需要一些时间来加载。当您再次向上滚动时,被移出屏幕的顶部项目现在也需要一些时间才能显示。

使用 WebView 时有没有办法提高性能?有没有办法消除这种视觉加载时间?我们应该考虑哪些方法?感谢您的任何帮助。

I have a large drilldown menu in my iOS app.
The menu content originated from XML.
It's stored in an SQLite database and Webview is being to used.
Why is Webview being used? Because the text has styling (font size/ color).

Here's the problem: When you scroll down the menu, the text bars that were previously offscreen remain invisible for a 1/2 second. They're taking awhile to load. And you scroll up again, the top items that were moved off screen now take time to appear as well.

Is there a way to improve this performance when working with WebView? Is there anyway to eliminate this visual loading time? Are there approaches we should consider? Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2024-12-06 08:32:42

您是否将 Web 视图放入 UITableView 中?你真的不能这么做;他们会互相争斗(这被明确记录为不支持)。

主要有两种解决方案:将这部分程序重新设计为Web应用程序,或者使用Core Text进行布局。在单个 UIWebView 中完成所有工作要快得多,但需要在 JavaScript 和 Objective-C 之间移动,这很难开发和调试。如果您采用这种方式,我通常建议您将 JavaScript 部分构建为“迷你应用程序”,您可以在 Safari 中开发和调试,然后插入。

对于简单的布局,Core Text 并不太难。如果您创建一个 CFAttributedString,那么您需要绘制一个非常简单的框架:

- (id)initWithFrame:(CGRect)frame {
  if ((self = [super initWithFrame:frame])) {
    CGAffineTransform 
    transform = CGAffineTransformMakeScale(1, -1);
    CGAffineTransformTranslate(transform, 
                               0,
                               -self.bounds.size.height);
    self.transform = transform;
    self.backgroundColor = [UIColor whiteColor];
  }
  return self;
 }

- (void)drawRect:(CGRect)rect {
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetTextMatrix(context, CGAffineTransformIdentity);

  CGPathRef path = CGPathCreateWithRect(self.bounds, NULL);

  CFAttributedStringRef
  attrString = (__bridge CFTypeRef)self.attributedString;

  // Create the framesetter using the attributed string
  CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);

  // Create a single frame using the entire string (CFRange(0,0))
  // that fits inside of path.
  CTFrameRef
  frame = CTFramesetterCreateFrame(framesetter,
                                   CFRangeMake(0, 0),
                                   path, 
                                   NULL);

  // Draw the frame into the current context
  CTFrameDraw(frame, context);

  CFRelease(frame);
  CFRelease(framesetter);
  CGPathRelease(path);
}

以及所需的插件: 是的,我的书将包含 20 多页各种花哨的文本布局,大部分使用 Core Text (今天刚刚完成该章)。但对于简单的东西来说,只要不需要剪切和粘贴,上面的内容就足够了。

Are you putting web views inside of a UITableView? You really can't do that; they'll fight each other (and this is explicitly documented as not supported).

There are two main solutions: redesign this part of the program as a web app, or layout with Core Text. Doing all the work in a single UIWebView is much faster, but requires moving between JavaScript and Objective-C which is difficult to develop and debug. If you go this way, I generally recommend that you build the JavaScript part as a "mini-app" that you can develop and debug in Safari, and then drop-in.

For simple layout, Core Text isn't too hard. If you create a CFAttributedString, here's all you need to draw a very simple frame:

- (id)initWithFrame:(CGRect)frame {
  if ((self = [super initWithFrame:frame])) {
    CGAffineTransform 
    transform = CGAffineTransformMakeScale(1, -1);
    CGAffineTransformTranslate(transform, 
                               0,
                               -self.bounds.size.height);
    self.transform = transform;
    self.backgroundColor = [UIColor whiteColor];
  }
  return self;
 }

- (void)drawRect:(CGRect)rect {
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetTextMatrix(context, CGAffineTransformIdentity);

  CGPathRef path = CGPathCreateWithRect(self.bounds, NULL);

  CFAttributedStringRef
  attrString = (__bridge CFTypeRef)self.attributedString;

  // Create the framesetter using the attributed string
  CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);

  // Create a single frame using the entire string (CFRange(0,0))
  // that fits inside of path.
  CTFrameRef
  frame = CTFramesetterCreateFrame(framesetter,
                                   CFRangeMake(0, 0),
                                   path, 
                                   NULL);

  // Draw the frame into the current context
  CTFrameDraw(frame, context);

  CFRelease(frame);
  CFRelease(framesetter);
  CGPathRelease(path);
}

And the required plug: Yes, my book will include over 20 pages on all kinds of fancy text layout, mostly using Core Text (just finishing that chapter today). But for the simple stuff the above is enough, as long as you don't need cut and paste.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文