Waiting for a specified duration in Cocoa

前端 未结 7 1986
生来不讨喜
生来不讨喜 2020-12-29 03:41

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 ma         


        
7条回答
  •  -上瘾入骨i
    2020-12-29 04:00

    Here's the NSTimer way of doing it. It's might be even uglier than the method you're using, but it allows repeating events, so I prefer it.

    [NSTimer scheduledTimerWithTimeInterval:0.5f 
                                     target:self
                                   selector: @selector(doSomething:) 
                                   userInfo:nil
                                    repeats:NO];
    

    You want to avoid something like usleep() which will just hang your app and make it feel unresponsive.

提交回复
热议问题