33

So I'm developing my first iOS application and I need help..

Simple program for now, I have about 9 buttons and when I press the first button, or any button, I just want the first button to highlight for 60 milliseconds, unhighlight, second button highlights, wait 60 milliseconds, unhighlight and so on for the rest of the buttons so it looks like a moving LED.

I've looked tried sleep/usleep but once that sleep duration is done it seems like it skips the highlight/unhighlight all together.

For example:

- (void) button_circleBusy:(id)sender{
firstButton.enabled = NO;
sleep(1);
firstButton.enabled = YES;

and so on for the rest of the buttons. It DOES delay, but it doesn't delay the "firstButton.enabled = NO;". I have a picture for each button's "disabled state" and I never see it.

Any help's appreciated! I've looked into NSTimer but I was unsure on how to implement it.

Thanks.

-Paul

gcamp
  • 14,622
  • 4
  • 54
  • 85
Retro
  • 333
  • 1
  • 3
  • 5

7 Answers7

51

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];
}
...
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • Hey ughoavgfhw, thanks for replying. Alright that seems easy enough! But I also have another function called button_killAll() where it should disable everything for .06 seconds. I noticed how the NSTimer uses selector:@selector(goToSecondButton:), what if I don't want to go to another function and just delay the disable for .06 seconds? I guess another way to ask this question is that once I reach the last button to highlight/disable, what should I put since I don't want that loop to end? – Retro May 14 '11 at 00:01
47
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)
});
Avba
  • 14,822
  • 20
  • 92
  • 192
  • You are awesome! Thanks, I've been searching for this for ages! – Finn Gaida Sep 08 '13 at 01:31
  • I like this syntax too, thanks for saving my time. Anyway, if delay in seconds is less than a second, you should use delayInSeconds * NSEC_PER_MSEC instead and change delay value to 600 – majorl3oat Feb 27 '14 at 02:01
  • this works better than any other code of the answers +1 – Jesus Oct 31 '14 at 01:43
29

Less code is better code.

[NSThread sleepForTimeInterval:0.06];

Swift:

Thread.sleep(forTimeInterval: 0.06)
Alex Haas
  • 2,127
  • 2
  • 17
  • 13
  • Much better. Thanks. –  Oct 14 '13 at 08:05
  • 1
    Only after "Readable code is better than unreadable code", which is an even more important advocate of this answer. – Jesse the Game Nov 18 '13 at 17:09
  • I love the less code, but it doesn't allow the screen to update as the questioner requires. From the doc on NSThread sleepForTimeInterval: `No run loop processing occurs while the thread is blocked.` – slothbear Apr 16 '14 at 02:27
  • 2
    The sleep blocks the thread. So there will be sideeffects and it is not useful for UI Threads. – Karsten Apr 28 '15 at 11:44
25

A slightly less verbose way is to use the performSelector: withObject: afterDelay: which sets up the NSTimer object for you and can be easily cancelled

So continuing with the previous example this would be

[self performSelector:@selector(goToSecondButton) withObject:nil afterDelay:.06];

More info in the doc

16

Try

NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.06 ];
[NSThread sleepUntilDate:future];
Joe Völker
  • 781
  • 1
  • 5
  • 19
3

I would like to add a bit the answer by Avner Barr. When using int64, it appears that when we surpass the 1.0 value, the function seems to delay differently. So I think at this point, we should use NSTimeInterval.

So, the final code is:

NSTimeInterval delayInSeconds = 0.05;

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

//do your tasks here

});
Community
  • 1
  • 1
John Bassos
  • 308
  • 1
  • 10
0
[NSTimer scheduledTimerWithTimeInterval:.06 target:self selector:@selector(goToSecondButton:) userInfo:nil repeats:NO];

Is the best one to use. Using sleep(15); will cause the user unable to perform any other actions. With the following function, you would replace goToSecondButton with the appropriate selector or command, which can also be from the frameworks.

Hanson So
  • 167
  • 2
  • 4