6

I am working on a simple quiz game and i want to add some little delays to the game,now when i select the answer the game goes instantly to the next answer now i want to change the color of the button for 0.1 seconds and then loads the next question

I tried the sleep function but it adds only the delay without the color change and i can't choose time intervals smaller than a second because it accepts integers as value

here is the code

sender.backgroundColor = UIColor.greenColor()
sleep(1)
sender.backgroundColor = UIColor.whiteColor()

what i have to put instead of sleep to obtain what i want?

thanks

4 Answers 4

5

If you only need a sleep function, just use

NSThread.sleepForTimeInterval(1)
Sign up to request clarification or add additional context in comments.

2 Comments

i tried put this line between the two change color instructions,it adds the delay but doesn't change color
Want to make it clear that this blocks the thread. As well as sleep() does. This does not solve GioB's issue.
2

Use usleep which takes an int in microseconds. (i.e. 1,000,000 microseconds is equivalent to 1 second) So for 0.1s use:

       // Sleep for 0.1s
       usleep(100000) 

Recommend to use in a background thread. You certainly don't want to be doing this on the main UI Thread!

Comments

1

You can use NSTimer for that, firstly you implement NSTimer and you add duration time 1.0 second or what ever want then, pass the time NSTimer call its function and you change questions to another

1 Comment

i was already using timers,i added another one for the change of the color and it worked, thank you
0

I think you should try NSTimer or dispatch_after to do such things:(The NSTimer may be not so convenient as it needs a class method used as call back selector)

sender.backgroundColor = UIColor.greenColor()
dispatch_after(#your time#, 
  dispatch_get_main_queue()){
  sender.backgroundColor = UIColor.whiteColor()
  #load your new question logic#
}

P.S.:The performSelector:delay: method is not available in Swift.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.