在Cocoa中等待指定的持续时间 [英] Waiting for a specified duration in Cocoa

查看:136
本文介绍了在Cocoa中等待指定的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个更直接的方法来等待可口可乐比下面我想到的一个特定的时间。

Is there a more straightforward way to wait for a specific amount of time in Cocoa than what I have come up with below?

- (void) buttonPressed {
    [self makeSomeChanges];

    // give the user some visual feedback and wait a bit so he can see it
    [self displayThoseChangesToTheUser];
    [self performSelector:@selector(buttonPressedPart2:) withObject:nil afterDelay:0.35];
}

- (void) buttonPressedPart2: (id)unused {
    [self automaticallyReturnToPreviousView];
}

为了清楚起见,没有这个代码的问题 - 我唯一的牛肉是一种风格。在我的情况下,流量是足够简单,它的工作,但尝试封装它或抛出一些条件,事情可能变得丑陋。对我来说有点of叨,我找不到一种方法等待,然后返回到像这样的(虚构的)例子中的同一点:

Just to be clear, there is no functional problem with this code -- my only beef is a stylistic one. In my case the flow is simple enough that it works, but try to encapsulate it or throw in some conditionals and things could turn ugly. It's been kind of nagging at me that I couldn't find a way to wait and then return to that same point in code like this (fictitious) example:

- (void) buttonPressed {
    [self doStuff];
    [UIMagicUnicorn waitForDuration:0.35];
    [self doStuffAfterWaiting];
}


推荐答案



There is

usleep(1000000);

[NSThread sleepForTimeInterval:1.0f];

两者都将睡眠1秒钟。

这篇关于在Cocoa中等待指定的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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