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

查看:267
本文介绍了强制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.

这里是代码片段的数据提供给视图: / p>

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];

干杯,
Doug

Cheers, Doug

推荐答案

强制 UIView 重新渲染的有保证的,坚实的方法是 [myView setNeedsDisplay] / code>。如果您遇到问题,可能会遇到以下问题之一:

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:延迟为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:挖掘你的视图层次结构,并确保你不试图outsmart是在某个地方。过度聪明是我的经验中坏的绘画的第一个原因。当你认为你最好地知道如何欺骗系统做你想要的,你通常得到它正在做你不想要的。

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天全站免登陆