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 02-03-2012, 05:08 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 14
iph_s is on a distinguished road
Default How to achieve that effect ?

I have an array that contains numbers from 1 to 200.
What I want is these numbers to be randomly displayed in UILabel for let's say 5 seconds and at the end only one number to be displayed (let's say the winner number).
How to do that ?
iph_s is offline   Reply With Quote
Old 02-03-2012, 05:16 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 80
itsaboutcode is on a distinguished road
Default

Quote:
Originally Posted by iph_s View Post
I have an array that contains numbers from 1 to 200.
What I want is these numbers to be randomly displayed in UILabel for let's say 5 seconds and at the end only one number to be displayed (let's say the winner number).
How to do that ?
You have to create a loop which will run five times. Once you have found the random number, display it in UILabel and after that wait for 5 sec.

To Find the random number: Random number in iphone sdk? - Stack Overflow

To wait for n sec, check this: Sleep, Pause or Block a Thread
itsaboutcode is offline   Reply With Quote
Old 02-03-2012, 05:22 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 14
iph_s is on a distinguished road
Default

This is not what I looking for. I know how to make random generator etc, but I don't know how to animate the process. I want to know how to animate the numbers in UILabel.
iph_s is offline   Reply With Quote
Old 02-03-2012, 05:33 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 80
itsaboutcode is on a distinguished road
Default

Quote:
Originally Posted by iph_s View Post
This is not what I looking for. I know how to make random generator etc, but I don't know how to animate the process. I want to know how to animate the numbers in UILabel.
Checkout following links - I hope they will help you out.

How to use iPhone Timers and Animated Views

ipad - setting new text to uilabel and animation - Stack Overflow

ipad - iPhone UILabel animation - Stack Overflow
itsaboutcode is offline   Reply With Quote
Old 02-03-2012, 05:34 AM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iph_s View Post
This is not what I looking for. I know how to make random generator etc, but I don't know how to animate the process. I want to know how to animate the numbers in UILabel.
Record the current time to an NSTimeInterval instance variable:

NSTimeInterval startTime;

Code:
startTime = [NSDate timeintervalSinceReferenceDate];
Start an NSTimer that fires say, 4 times a second. Have that timer call a method, changeLabel:. Make your changeLabel method follow the signature for timer methods (it needs to have 1 parameter: the timer itself.)

In your changeLabel method, pick a random number, use it as an index into your array, and install the value at that index into your label.

Check to see if your time period (5 seconds) has elapsed. If it has, kill your timer.

Code:
if ([NSDate timeintervalSinceReferenceDate] - startTime >= 5)
   [theTimer invalidate];
Are you asking how to do some sort of animation for updating your label? If so, what kind of animation?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-03-2012, 06:40 AM   #6 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 14
iph_s is on a distinguished road
Default

Here is what I want to achieve. Please see the animated gif.

iph_s is offline   Reply With Quote
Old 02-03-2012, 06:52 AM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iph_s View Post
Here is what I want to achieve. Please see the animated gif.


See my post then. I told you exactly what to do.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-03-2012, 11:49 AM   #8 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

you'll need to do the animations in a background thread

put this lin in viewDidLoad:

[self performSelectorInBackground:@selector(foo) withObject:nil];


then add this function:

-(void)foo
{
NSAutoreleasePool *aPool = [NSAutoreleasePool alloc] init];

// do the work

[aPool drain];
}
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 02-03-2012, 11:57 AM   #9 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

Quote:
Originally Posted by RickSDK View Post
you'll need to do the animations in a background thread

put this lin in viewDidLoad:

[self performSelectorInBackground:@selector(foo) withObject:nil];


then add this function:

-(void)foo
{
NSAutoreleasePool *aPool = [NSAutoreleasePool alloc] init];

// do the work

[aPool drain];
}
If it is intensive work, he should do the work in a background thread and update the user interface on the main thread.
Speed is offline   Reply With Quote
Old 02-04-2012, 07:13 AM   #10 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 14
iph_s is on a distinguished road
Default

@Duncan C,
Thans a lot !
iph_s is offline   Reply With Quote
Old 02-04-2012, 07:45 AM   #11 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
you'll need to do the animations in a background thread

put this lin in viewDidLoad:

[self performSelectorInBackground:@selector(foo) withObject:nil];


then add this function:

-(void)foo
{
NSAutoreleasePool *aPool = [NSAutoreleasePool alloc] init];

// do the work

[aPool drain];
}
This is absolutely and completely wrong. Do not follow this advice.

What the OP asked for was a simple cycle of values. It is not appropriate for a background thread for 2 reasons:
  • Picking a random item and updating a label take almost no time.
  • All changes to the UI need to be done on the main thread. Altering UI Elements in the background causes unexpected results, and even crashes.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

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: 395
18 members and 377 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, imac74, ipodphone, jeroenkeij, kukat, LunarMoon, MAMN84, n00b, QuantumDoja, reficul, Retouchable, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

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