简体   繁体   English

Xcode iOS目标C线程

[英]Xcode ios objective c Thread

I want to make an simple ios app. 我想制作一个简单的ios应用。

I want to click on the button and change the backgroundcolor of buttons in a view. 我想单击按钮并更改视图中按钮的背景色。

It already is working, but i want to do it asnyc. 它已经在工作,但是我想做asnyc。

For example. 例如。

Buttton1 grey Wait a second Button2 grey Wait And so on. Buttton1灰色,等待第二个Button2灰色,等待,依此类推。

This is the base source: 这是基本来源:

int i = 1;
UIView *view = viewButtons;
for(UIButton *btnItem in view.subviews)



    NSString *strValue = [@(i) stringValue];
    btnItem.backgroundColor = [UIColor lightGrayColor];
    [btnItem setTitle: strValue forState:UIControlStateNormal];
    i++;

    usleep(1000 * 1000);

Can anyone help me? 谁能帮我? Thanks! 谢谢!

Take a look at NSTimer . 看看NSTimer It will allow you to specify an interval and whether you want to repeat the action or not. 它将允许您指定间隔以及是否要重复操作。 As Hot Licks mentioned, there are other ways to do it. 正如Hot Licks提到的那样,还有其他方法可以做到这一点。 If you decide to create the thread on your own, eg using GCD , remember that UI updates must be always made on the main thread. 如果您决定自己创建线程,例如使用GCD ,请记住,UI更新必须始终在主线程上进行。

You could use either NSTimer (as previously mentioned) or a dispatch_after() call. 您可以使用NSTimer(如前所述)或dispatch_after()调用。 If it's a repeating action, use an NSTimer and set it to repeat. 如果是重复操作,请使用NSTimer并将其设置为重复。 If it's a non-repeating action, you could either use an NSTimer set not to repeat, or a dispatch_after() call. 如果这是一个非重复操作,则可以使用NSTimer设置为不重复,也可以使用dispatch_after()调用。 Up to you. 由你决定。 Definitely do not thread UI code; 绝对不要线程UI代码; that has undefined behavior (in other words, it will not work properly). 具有不确定的行为(换句话说,它将无法正常工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM