ios - Dispatch and Delay between calls -
i'm making clone old game simon (a.k.a. genius, in brazil), 1 coloured buttons player needs press following sequence of colors.
for testing, interface has 4 coloured buttons created array button outlets, easy access:
var buttonarray:[uibutton] = [self.greenbutton, self.yellowbutton, self.redbutton, self.bluebutton]
also, created array store sequence of colors
var colors:[int] = []
when game starts calls function adds random number 0 3 (index on buttonarray), , add number colors array after adding new color color sequence, app needs show sequence user, can repeat it
for that, calls playmoves function, uses loop run through colors array , change alpha button, simulating 'blink'
func playmoves(){ let delay = 0.5 * double(nsec_per_sec) let time = dispatch_time(dispatch_time_now, int64(delay)) in self.colors{ self.buttonarray[i].alpha = 0.2 dispatch_after(time, dispatch_get_main_queue(), { self.buttonarray[i].alpha = 1 }) } }
it changes alpha button 0.2 , then, after half second returns alpha 1. using dispatch_after, passing 0.5 seconds , on code block returns alpha, guys can see on code above.
on first run, appears correctly, when colors array has 2 or more items, when runs loop, although has 0.5 sec delay, blinks buttons on same time.
it's dumb mistake i'm making, i'm clueless on moment.
i appreciate help!
thanks!
all of these dispatch_after
calls scheduled @ same time, making them appear blink @ same time. there couple of approaches solve this:
you could, example, adjust when
parameter (the dispatch_time_t
parameter) each button offset original time (such delay
was, i * 0.5 * double(nsec_per_sec)
).
you use key-frame animation, i'd suggest first try fixing delay
in dispatch_after
approach first.
Comments
Post a Comment