一次又一次地更新UIVIew [英] Update a UIVIew again and again and again

查看:66
本文介绍了一次又一次地更新UIVIew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的iPhone应用程序,其唯一功能是永久更新UIView(直到退出).

I'm making a simple iPhone app whose sole function is to update a UIView forever (until it exits).

我在applicationDidFinishLaunching和viewDidLoad中尝试了此操作

I tried this in applicationDidFinishLaunching and viewDidLoad:

while(1) {  
    // update view here  
}

但这不起作用-应用程序永远不会完成加载.我敢肯定有一个简单的解决方案,我只是不知道它的作用.

but that doesn't work- the app never finishes loading. I'm sure there's a simple solution to this, I just don't know what it its.

也:理想情况下,此过程应消耗很少的资源.

Also: ideally, this process should consume very little resources.

推荐答案

您不能像此处那样使用 while(1)语句,因为它不允许viewDidLoad返回,并且您的应用将再也不会接到任何其他呼叫,例如点击处理,屏幕绘制更新等.

You can't have a while (1) statement like there, as it not allow viewDidLoad to return, and your app will never get any other calls such as tap processing, screen draw updates, etc.

在viewDidLoad中,使用以下命令设置计时器任务:

In viewDidLoad, set up a timer task using:

updateTimer = [NSTimer scheduledTimerWithTimeInterval: kUpdateTimeInterval target:self selector:@selector(updateView) userInfo:nil repeats:YES];

并有一个名为 updateView 的方法来实际执行更新.除了 updateView 之外,您还可以使用 setNeedsDisplay ,它会触发对视图类的-drawRect方法的调用,并在那里进行实际绘制.

And have a method called updateView that actually does the updating. Instead of updateView you could also use setNeedsDisplay which will trigger a call the -drawRect method of your view class, and do the actual drawing there.

现在最终发生的是,您的viewDidLoad将设置一个重复任务,并且在每个kUpdateInterval处,您的视图都将被更新.

What ends up happening now is that your viewDidLoad will set up a repeating task and at every kUpdateInterval, your view will be updated.

这篇关于一次又一次地更新UIVIew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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