Quote:
Originally Posted by LiquidChaz
I'm having trouble finding just a basic tutorial on how to use NSTimer properly.
I have about 10 different UILabels that I want to "animate" by changing their background colors in a pre-set sequence. Could someone help me with some sample code to get this done properly with NSTimer?
|
OK I like to make a BOOL value that tells you if the timer is ticking or not. It basically adds pause functionality. Ad an int value for how many ticks have passed.
Code:
BOOL isTimerTicking;
int numTimerTicks;
When your view loads, start your timer at an interval of your choice.(here it's 1 second)
Code:
[[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerTick) userInfo:nil repeats:YES];
numTimerTicks = 0;
isTimerTicking = YES;
inside that file make a new method matching the selector in your NSTimer.
Code:
-(void)timerTick
{
if(isTimerTicking)
{
//What ever code is in here will be called every time interval if your timer isn't paused by you
//change the colour!
if(numTimerTicks == 5)
{
[myLabel setBackgroundColor:...];
}
else if(numTimerTicks == 6)
{
[myOtherLabel setBackgroundColor:...];
}
else if(numTimerTicks > 6)
{
//"pause" timer, we're done
isTimerTicking = NO;
}
numTimerTicks++;
}
}
to "pause" the timer set isTimerTicking to NO.
If you'd like you could make a method that begins this animation for you:
Code:
-(void)animateLabels
{
numTimerTicks = 0;
isTimerTicking = YES;
}
If you want a more accurately timed animation lower the time interval it ticks at.