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-21-2009, 11:34 AM   #1 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 79
joshholat is on a distinguished road
Default For Loop Timer

Hey all,

I have a For Loop in which I am trying to run 52 times. I want it to run once, pause a few seconds, run the second time, pause a few seconds, etc. I know that sleep() isn't good to use, but I can't seem to get NSTimer to work since it requires a method to execute after it is out of time. Is there anyway to make NSTimer pause the execution and then execute the local for loop rather than a function? Is there a different way to pause during each execution in the for loop? Thanks!
joshholat is offline   Reply With Quote
Old 02-21-2009, 11:36 AM   #2 (permalink)
Tutorial Author
 
Join Date: Jan 2009
Posts: 144
meowmix23F is on a distinguished road
Default

You can make the loop longer by doing this...

for ( int i = 0; i < someNumber; i++ )
{
// do stuffs
while ( int j < someBigNumber )
{
j++;
}
}
meowmix23F is offline   Reply With Quote
Old 02-21-2009, 11:38 AM   #3 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 79
joshholat is on a distinguished road
Default

Quote:
Originally Posted by meowmix23F View Post
You can make the loop longer by doing this...

for ( int i = 0; i < someNumber; i++ )
{
// do stuffs
while ( int j < someBigNumber )
{
j++;
}
}
The only problem with that is how inaccurate it would be, I need a set time interval that is fairly accurate.
joshholat is offline   Reply With Quote
Old 02-21-2009, 12:41 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by joshholat View Post
Hey all,

I have a For Loop in which I am trying to run 52 times. I want it to run once, pause a few seconds, run the second time, pause a few seconds, etc. I know that sleep() isn't good to use, but I can't seem to get NSTimer to work since it requires a method to execute after it is out of time. Is there anyway to make NSTimer pause the execution and then execute the local for loop rather than a function? Is there a different way to pause during each execution in the for loop? Thanks!
NSTimer is definitely what you want - what's wrong with it? instead of for(int i = 0;i<someNumber; i++) , you'll want to put the variable "i" at the class level (and give it a meaningful name, like "frameCount").

When you start the timer, set frameCount to 0.

When the timer fires, have it call a method that (1) performs your task (2) adds one to frameCount (3) checks if frameCount>someNumber; if so, invalidate the timer and call another method for whatever happens after the loop.

Don't use a long loop to cause a delay - that will keep your main thread from performing other tasks, like responding to touches, phone calls, and other events.
__________________

Free Games!

Last edited by smasher; 02-21-2009 at 01:22 PM. Reason: typos
smasher is offline   Reply With Quote
Old 02-21-2009, 01:21 PM   #5 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 79
joshholat is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
When you start the timer, set frameCount to 0.

When the timer fires, have it call a method that (1) performs your task (2) adds one to frameCount (3) checks if frameCount>someNumber; if so, invalidate the timer and call another method for whatever happens after the loop.
I'm not sure I follow you... I want a for loop that pauses for like 3 seconds at the end of each execution before it starts for the next iteration. Is the following like what you are describing?

- (void)startTimer {
frameCount = 0;
[NSTimer *pauseTimer = scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myFunction userInfo:nil repeats:NO]
}

- (void)myFunction {
for (int x = 0; x < 52; x++) {
//for loop code here
frameCount++;
if (frameCount > someNumber)
[pauseTimer invalidate];
}
}
joshholat is offline   Reply With Quote
Old 02-21-2009, 10:51 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

It's not clear to me if you:
(A) want to do something 52 times, pausing for 3 seconds after EACH SOMETHING (total time, 52x3 seconds)

(B) want to do something 52 times, pause for 3 seconds, then repeat (B) (total time: who knows.)

I'm gonna go with (A) because it's the harder question.
declare "int frameCount" and "NSTimer *pauseTimer" in your .h file.
then:

Code:
// this was almost OK - 
//I just removed the local declaration of pauseTimer, 
//took the colon off of myFunction (because it takes no params) and set repeats:YES
- (void)startTimer {
	frameCount = 0;
	pauseTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
}

- (void)myFunction {	
	//Do your thing here -
	//whatever you want to happen every 3 seconds. 
	NSLog(@"Doing My thing.");
	
	//count by one, and check if we're done.
	frameCount++;
	if (frameCount >= 52)
		[pauseTimer invalidate];
}
If you wanted (B) instead , just add your loop inside myFunction, and remove or adjust the "if" statement.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-21-2009, 11:05 PM   #7 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 79
joshholat is on a distinguished road
Default

Thanks, that's exactly what I was looking for! And A was the correct choice, your description makes sense now. Thanks again!
joshholat is offline   Reply With Quote
Old 02-26-2010, 12:46 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
jbullfrog is on a distinguished road
Default sound loop with delay?

Hello, I have an if statement that I would like to add a loop with delay in it, But I cant seem to figure it out.

I have something like this.

Code:
if (myCount == 2) {

		
		NSLog(@"TAP 2");
		
		// Im playing a sound here. which plays fine.
                
                // I would like to add a timer or some sort of delay to fire the       sound to play again after a set amount of time.


}
I'm not sure If I should use an NStimer or a for loop, or something else.

Any help would be great!

Thanks in advance.
jbullfrog is offline   Reply With Quote
Old 02-26-2010, 02:11 PM   #9 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by jbullfrog View Post
Hello, I have an if statement that I would like to add a loop with delay in it, But I cant seem to figure it out.
{snip}
I'm not sure If I should use an NStimer or a for loop, or something else.
You shouldn't use a "for" loop or "sleep" to pause the main thread - that'll keep input events from being processed, etc. Use an NSTimer instead. The example code I posted should do it. If you only need it to repeat once then you don't need the variable frameCount, and you can set repeats:NO instead.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-26-2010, 02:30 PM   #10 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
jbullfrog is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
You shouldn't use a "for" loop or "sleep" to pause the main thread - that'll keep input events from being processed, etc. Use an NSTimer instead. The example code I posted should do it. If you only need it to repeat once then you don't need the variable frameCount, and you can set repeats:NO instead.
Smasher, thanks for the quick reply, I guess where my hang up is that I have something like this.

Code:
- (IBAction)pushButton1 {
	
	if (isLooping == YES){
		
		// Stop looping sound
		isLooping = NO;
		NSLog(@"Looping stopped");
	}
	
	else if (myCount == 1) {
		
		
		
		NSLog(@"TAP 1");
	
		//play sound once

	}
	else if (myCount == 2) {

		
		NSLog(@"TAP 2");
		
		
		//play sound once

                
        // I would like to add a timer or some sort of delay to fire the       
	   // sound to play again after a set amount of time.
		
		isLooping = YES;

				
	}
	
	
}
So the NSTimer calls a function, then how do I add the function inside my else if (myCount == 2) statment?

Would I do something like this?

Code:
- (void)startTimer {
	pauseTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
}

- (IBAction)pushButton1 {
	
	if (isLooping == YES){
		
		// Stop looping sound
		isLooping = NO;
		NSLog(@"Looping stopped");
	}
	
	else if (myCount == 1) {
		
		
		
		NSLog(@"TAP 1");
	
		//play sound once

	}
	else if (myCount == 2) {

		
		NSLog(@"TAP 2");
		
- (void)myFunction {	
	//play sound once
	//this will happen every 3 seconds. 
	NSLog(@"Doing My thing.");
        
        [pauseTimer invalidate];
}
		
		isLooping = YES;

				
	}
	
	
}
Also I want the timer to start when the else if (myCount == 2) statment is called, not when the app starts if that makes sense.

Thanks again for your help.
jbullfrog is offline   Reply With Quote
Old 02-26-2010, 02:50 PM   #11 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The line with "scheduledTimerWithTimeInterval" is the one that starts the timer, so either that line should go inside the if (myCount == 2) statement or you should put [self startTimer] inside the if statement.

The method "myFunction" should go *outside* the pushButton1 method. You don't write one method inside another (although you can, of course, *call* one method from inside another.)
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-26-2010, 03:45 PM   #12 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
jbullfrog is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
The line with "scheduledTimerWithTimeInterval" is the one that starts the timer, so either that line should go inside the if (myCount == 2) statement or you should put [self startTimer] inside the if statement.

The method "myFunction" should go *outside* the pushButton1 method. You don't write one method inside another (although you can, of course, *call* one method from inside another.)
Thanks Smasher!

I got it all worked out now, the only thing that I'm faced with now is that, the sounds are in a Scroll View, and once the user hits the button the the sound is played, a "for" loop is fired to play a small animation, then the timer starts and after a set period of time it calls the method "myFunction" which then plays the sound again and the small animation. It all works how I want it to except when I start to scroll, it seems to be pausing the timer and then when I stop scrolling the method "myFunction" is fired. This is a pretty big issue.

I know its not how im playing the sound or the animation, because when I set that to just loop via the sound manager the sound and animation play fine while scrolling.

Any thoughts on this?
jbullfrog is offline   Reply With Quote
Old 02-26-2010, 08:25 PM   #13 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
jbullfrog is on a distinguished road
Default UIScrollView pauses NSTimer until scrolling finishes

So I found a little info about my issue with UIScrollView pauses NSTimer until scrolling finishes, but im not sure that the " NSRunLoop method:" is or how to use it.

"this is a run loop issue. Specifically, you need to make use of the NSRunLoop method:"

UIScrollView pauses NSTimer until scrolling finishes - Stack Overflow

Any thoughts?
jbullfrog is offline   Reply With Quote
Reply

Bookmarks

Tags
delay, for loops, nstimer, pause

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: 331
4 members and 327 guests
guusleijsten, HowEver, LEARN2MAKE, mottdog
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
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 08:42 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0