如何强制视图渲染自己? [英] How to force a view to render itself?

查看:140
本文介绍了如何强制视图渲染自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有一些小的子视图。当用户点击子视图时,我会在点击的子视图(相同大小)的顶部放置一个单独的视图,然后将此单独视图设置为全屏大小。

In my app I have a number of small subviews. When the user taps a subview, I place one separate view over top of the tapped subview (same size) and then animate this separate view to full-screen size.

放大视图是自定义绘制的,这会产生这种方法的一个大问题。如果我在主视图的 viewDidLoad 方法中创建放大视图并将其帧设置为全屏大小,则视图的 drawRect 方法,并且视图正确呈现为全尺寸。如果我然后在动画中使用此视图,一切都按预期工作。

The enlarging view is custom-drawn, which is creating a big problem with this approach. If I create the enlarging view in my main view's viewDidLoad method and set its frame to full-screen size, the view's drawRect method is called immediately, and the view is properly rendered full-size. If I then use this view in the animation, everything works as expected.

但是,我需要这个放大视图根据点击的子视图而有所不同,所以我需要每次重新创建/重新绘制它。问题是当我这样做以响应触摸事件时,视图的 drawRect 方法在动画开始之前不会被调用,此时视图的帧很小而不是全屏,因此一切都被绘错了。

However, I need this enlarging view to be different depending on which subview was tapped, so I need to re-create/re-draw it each time. The problem is that when I do this in response to a touch event, the view's drawRect method does not get called until the animation begins, at which time the view's frame is small instead of full-screen, and as a result everything is drawn wrong.

在我的触摸事件代码中,我明确地将视图的框架设置为全屏并调用 setNeedsDisplay 在开始动画之前,但这没有效果 - drawRect 直到动画开始才调用。

In my code for the touch event, I'm explicitly setting the view's frame to fullscreen and calling setNeedsDisplay before starting the animation, but this has no effect - drawRect just isn't called until the animation begins.

有没有办法强制重绘,以便在其框架是我想要的尺寸时绘制视图?

Is there any way to force a redraw, so that the view is drawn while its frame is the size I want it to be?

推荐答案

正如我上面评论的那样,Joe问了类似的问题(作为更一般的问题)提供了一个操作当前运行循环的答案。

As I commented above, Joe asked something similar (as a more general question) here, to which Tom Swift provided an answer that manipulates the current run loop.

他的建议是强制运行使用以下代码调用 -setNeedsDisplay 后立即循环一次迭代:

His suggestion was to force the run loop through one iteration immediately after you call -setNeedsDisplay using the following code:

[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];

这种方法有一些注意事项,在那里的评论中讨论过。

There are some caveats to this approach, which are discussed in the comments there.

另外,有人指出那里

[CATransaction flush];

也可能达到强制立即重绘视图的效果。

may also achieve the same effect of forcing an immediate redraw of the view.

这篇关于如何强制视图渲染自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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