Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2009, 12:04 PM   #1 (permalink)
Registered Member
 
LiquidChaz's Avatar
 
Join Date: Sep 2009
Location: New York City
Posts: 53
LiquidChaz is on a distinguished road
Default Using NSTimer to change Label background colors in sequence

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?
LiquidChaz is offline   Reply With Quote
Old 10-10-2009, 12:32 PM   #2 (permalink)
Registered Member
 
kierster's Avatar
 
Join Date: Mar 2009
Location: Canada!
Posts: 261
kierster is on a distinguished road
Default

Quote:
Originally Posted by LiquidChaz View Post
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.
__________________
Check out some of my apps:
Boltz ($0.99)
FreeBoltz (FREE)
Cross Digits ($2.99) [Universal!]
Cross Digits Lite (FREE) [Universal!]
Targets ($0.99) (Facebook | YouTube Demo)
Greg's Apps

Last edited by kierster; 10-10-2009 at 12:39 PM.
kierster is offline   Reply With Quote
Old 11-14-2010, 03:58 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Better late than not at all

Hey,
Thanks for taking the time to share this code. I had to remove one [ to get the code to work but that was a simple typo I guess.

Maybe the person you were helping got hit by a bus and could not thank you personally...

Rob
aldcorn@live.com is offline   Reply With Quote
Reply

Bookmarks

Tags
animate, label, nstimer, sequence, time

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 342
9 members and 333 guests
givensur, glenn_sayers, guusleijsten, ipodphone, mediaspree, mtl_tech_guy, Punkjumper, whitey99, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:35 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0