Xcode Objective-C | iOS: delay function / NSTimer help? Xcode Objective-C | iOS: delay function / NSTimer help? xcode xcode

Xcode Objective-C | iOS: delay function / NSTimer help?


sleep doesn't work because the display can only be updated after your main thread returns to the system. NSTimer is the way to go. To do this, you need to implement methods which will be called by the timer to change the buttons. An example:

- (void)button_circleBusy:(id)sender {    firstButton.enabled = NO;    // 60 milliseconds is .06 seconds    [NSTimer scheduledTimerWithTimeInterval:.06 target:self selector:@selector(goToSecondButton:) userInfo:nil repeats:NO];}- (void)goToSecondButton:(id)sender {    firstButton.enabled = YES;    secondButton.enabled = NO;    [NSTimer scheduledTimerWithTimeInterval:.06 target:self selector:@selector(goToThirdButton:) userInfo:nil repeats:NO];}...


int64_t delayInSeconds = 0.6;dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);dispatch_after(popTime, dispatch_get_main_queue(), ^(void){     do something to the button(s)});


Less code is better code.

[NSThread sleepForTimeInterval:0.06];

Swift:

Thread.sleep(forTimeInterval: 0.06)