强制 UIView 重绘的最健壮的方法是什么? [英] What is the most robust way to force a UIView to redraw?

查看:18
本文介绍了强制 UIView 重绘的最健壮的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有项目列表的 UITableView.选择一个项目会推送一个 viewController,然后继续执行以下操作.从方法 viewDidLoad 我为我的子视图所需的数据触发一个 URLRequest - 一个 UIView 子类,覆盖了 drawRect.当数据从云端到达时,我开始构建我的视图层次结构.有问题的子类被传递了数据,它的 drawRect 方法现在拥有它需要渲染的一切.

I have a UITableView with a list of items. Selecting an item pushes a viewController that then proceeds to do the following. from method viewDidLoad I fire off a URLRequest for data that is required by on of my subviews - a UIView subclass with drawRect overridden. When the data arrives from the cloud I start building my view hierarchy. the subclass in question gets passed the data and it's drawRect method now has everything it needs to render.

但是.

因为我没有明确调用 drawRect - Cocoa-Touch 处理它 - 我无法通知 Cocoa-Touch 我真的,真的希望这个 UIView 子类呈现.什么时候?现在就好了!

Because I don't call drawRect explicitly - Cocoa-Touch handles that - I have no way of informing Cocoa-Touch that I really, really want this UIView subclass to render. When? Now would be good!

我已经尝试过 [myView setNeedsDisplay].这有时有效.非常参差不齐.

I've tried [myView setNeedsDisplay]. This kinda works sometimes. Very spotty.

我已经为此纠结了好几个小时.谁能为我提供一种坚如磐石、有保证的方法来强制重新渲染 UIView.

I've be wrestling with this for hours and hours. Could someone who please provide me with a rock solid, guaranteed approach to forcing a UIView re-render.

以下是向视图提供数据的代码片段:

Here is the snippet of code that feeds data to the view:

// Create the subview
self.chromosomeBlockView = [[[ChromosomeBlockView alloc] initWithFrame:frame] autorelease];

// Set some properties
self.chromosomeBlockView.sequenceString     = self.sequenceString;
self.chromosomeBlockView.nucleotideBases    = self.nucleotideLettersDictionary;

// Insert the view in the view hierarchy
[self.containerView          addSubview:self.chromosomeBlockView];
[self.containerView bringSubviewToFront:self.chromosomeBlockView];

// A vain attempt to convince Cocoa-Touch that this view is worthy of being displayed ;-)
[self.chromosomeBlockView setNeedsDisplay];

干杯,道格

推荐答案

强制 UIView 重新渲染的有保证的、坚如磐石的方法是 [myView setNeedsDisplay].如果您遇到此问题,您可能会遇到以下问题之一:

The guaranteed, rock solid way to force a UIView to re-render is [myView setNeedsDisplay]. If you're having trouble with that, you're likely running into one of these issues:

  • 您在实际获得数据之前调用了它,或者您的 -drawRect: 过度缓存了某些东西.

  • You're calling it before you actually have the data, or your -drawRect: is over-caching something.

您期望在调用此方法时绘制视图.没有办法使用 Cocoa 绘图系统来要求立即绘图".这会破坏整个视图合成系统,破坏性能并可能产生各种伪影.只能说这需要在下一个绘制周期中绘制."

You're expecting the view to draw at the moment you call this method. There is intentionally no way to demand "draw right now this very second" using the Cocoa drawing system. That would disrupt the entire view compositing system, trash performance and likely create all kinds of artifacting. There are only ways to say "this needs to be drawn in the next draw cycle."

如果你需要的是一些逻辑,绘制,一些更多的逻辑",那么你需要将一些更多的逻辑"放在一个单独的方法中,并使用 -performSelector:withObject:afterDelay:-performSelector:withObject:afterDelay: 延迟为 0.这将在下一个绘制周期后放置更多逻辑".参见这个问题的例子那种代码,以及可能需要它的情况(尽管如果可能,通常最好寻找其他解决方案,因为它会使代码复杂化).

If what you need is "some logic, draw, some more logic," then you need to put the "some more logic" in a separate method and invoke it using -performSelector:withObject:afterDelay: with a delay of 0. That will put "some more logic" after the next draw cycle. See this question for an example of that kind of code, and a case where it might be needed (though it's usually best to look for other solutions if possible since it complicates the code).

如果您认为没有绘制内容,请在 -drawRect: 中放置一个断点,然后查看何时被调用.如果您正在调用 -setNeedsDisplay,但是 -drawRect: 没有在下一个事件循环中被调用,那么请深入查看您的视图层次结构并确保您没有试图智胜是某处.根据我的经验,过度聪明是导致不良绘画的第一大原因.当您认为自己最清楚如何诱使系统做您想做的事情时,您通常会让它做您不想要的事情.

If you don't think things are getting drawn, put a breakpoint in -drawRect: and see when you're getting called. If you're calling -setNeedsDisplay, but -drawRect: isn't getting called in the next event loop, then dig into your view hierarchy and make sure you're not trying to outsmart is somewhere. Over-cleverness is the #1 cause of bad drawing in my experience. When you think you know best how to trick the system into doing what you want, you usually get it doing exactly what you don't want.

这篇关于强制 UIView 重绘的最健壮的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆