Any tips on making five independent but simultaneous Countdown timers from 2 minutes down to 0, and then restarts at 2 min. And, whenever a user hits a reset button for each timer, that timer would reset to 2 min...
And, an alarm should sound when each timer reaches countdown below 10 sec..
Any help would be greatly appreciated!
:-)
Cheers,
loulou
How precise do you need your timers to be? If you can live with a tiny bit of uncertainly, go with NSTimers. Basically, you schedule 5 instances of NSTimer on your runloop. Each time through the loop, your app will check if a timer has fired, and perform some method if this is the case.
The nice thing about this approach is that you do not need to synchronize across multiple threads. You can schedule all of the timers on the main thread.
Related
I am writing an iPhone game. When the user makes his first move a timer kicks of with an interval of 0.01 seconds. A UILabel displaying the time also gets updated every time.
I noticed when testing on an iPod touch 2nd gen and an iPhone 3GS that the iPod was slower (after 20 seconds the iPhone displayed 00:20,00 and the iPod displayed ~00:10,00). Is there a way to make this more reliable? If I'm correct, the NSTimer should run on its ow thread and should not be blocked by user interaction.
JNK
The NSTimer documentation states:
Because of the various input sources a
typical run loop manages, the
effective resolution of the time
interval for a timer is limited to on
the order of 50-100 milliseconds. If a
timer’s firing time occurs while the
run loop is in a mode that is not
monitoring the timer or during a long
callout, the timer does not fire until
the next time the run loop checks the
timer. Therefore, the actual time at
which the timer fires potentially can
be a significant period of time after
the scheduled firing time.
So accuracy can be as bad as 0.1, not 0.01 seconds. Not to mention if your thread is blocked for some reason. So if your firing time is crucial you should be looking at other things. Read this SO post for kick-off. Apple had a metronome sample code (in which, obviously, timing is crucial) but I can't find it just now.
In any case, if you are implementing a timer with NSTimer, you should record your start time. Then, whenever you update your interface, simply take the difference of the current time and your start time (with NSDates).
make sure you're not basing a timer off of sleeps or delays. You should always update a timer based on things like number of clock ticks since program start or current time
sorry I'm not more familiar with your language
You can't rely on a timer to run exactly at the specified time intervals, so the time you are displaying should always be calculated by taking time interval differences. And I doubt that a timer on the iPhone can run every 1 ms, in Quartz it is possible to get a timer call every 16 ms or so, making 60 fps - so scheduling it at 1ms probably means "run as soon as possible", which might be quite different on different hardware.
I'm trying to create my own loadbar; It consists of 5 bars, sliding in from the bottom from the left to the right, then sliding out to the top in the same order. There should be a fixed amount of time (say, 0.2 seconds) between all of them sliding in.
Currently, i'm using a timer to start them one by one, and then let themselves repeat. However, if there's some lag during start or something, they get messed up. The first two or sometimes 3 bars go almost simultaneously, the gap is bigger, just name it and it happened. I currently just set fixed times when each animation should start, but some lag makes them go bogus.
Any given bar moves up in about 0.3 seconds, waits there for 0.2 seconds, then moves further up in 0.3 seconds. They start 0.2 second after their left neighbour, from left to right. Once they started, they just repeat it.
So, what could i use to coordinate them in such a way, that there will always be the same amount of time between them?
Other things i have considered was just using a lot of images (but that will need lots of images i suppose), or using Key Value Observing (KVO) to see when the previous bar is at the desired height.
According to the NSTimer documentation, NSTimer objects are not guaranteed to be realtime accurate on iOS.
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
It's an issue that effects NSTimer as well as OpenGL based games. Michael Daley mentions something similar in his book about how to set things up properly for an openGL based game. I suspect that this is suffering from the same similar situation.
That said, I would suggest preloading those images a adding them to the view just outside the bounds or frame of the visible view before you need then so that they are immediately available when you need them.
Addendum:
After thinking about this, I'm wondering if the C function wait() will help you here. It's a long shot, but perhaps it's worth a try. If not, then the answer may very well be that there is none.
Addendum 2:
I have no idea if this is going to help you either, but I found this page, which discusses pausing a program for less than a second.
I have an IBAction which starts a number of timers.
I would like to have a second or a pause for a certain time. How can I pause execution?
I know of the [self performSelector:#selector(someMEthod:) withObject:someObject afterDelay:1.0];
but how can I just cause a delay without calling anything?
Thanks
You can call sleep() or +[NSThread sleepForTimeInterval:] but please don't do that on the main thread. Pausing execution of the main thread means your app's UI will be blocked for that time and appear to the user as if it had crashed. If you blocked the main thread for more than a few seconds, Apple's watchdog timer would kill your app instantly.
See this blog post by Jeff LaMarche for more on this issue: http://iphonedevelopment.blogspot.com/2010/05/psa-respect-main-thread.html
You don't want to cause a delay without calling anything. It's bad for the UI, and does not fit Cocoa Touch's event driven paradigm.
Cut the routine in which you want to pause into (at least) two parts/halves. Have the first half set up a delayed call to the second half (perform selector with delay, timer, queue, etc.) and then return to the main loop. The OS will call the second part, later, after potentially doing useful stuff in the mean time (giving the user a responsive UI or saving battery life, catching up with background email, etc.)
You may have to learn how to save state between parts (loop variables, etc.), something that some new programmers seem to miss in their learning.
You can use sleep(int_No_of_Sec); // eg. sleep(1);
This is an embarrassing question but it involes Xcode and the iPhone SDK.
Why would I need to set more than one timer? And do 2 timers take up more 'tics' than 1 timer or are they all based on the same master internal clock?
Is it better to add 1 timer routine and just have it call all the things I need to make happen or have 5 or 6. I'm not new to programing but I'm completely new to xCode and just trying to wrap my head around it.
Eg. If I have a timer updating the clock on the screen, and I also want to update the GPS position, is there any reason not to have the same timer update both?
Thank you.
One is that timer delegate will operate in separate threads. So in your case, when only one timer is used, if the GPS acquisition takes to much time, your screen will feel unresponsive because the clock will be unregularly updated.
im trying to execute 2 actions on occurrence of their particular events
one is the animation occurring when the a timer with fixed interval is fired
and other is also a animation occurring when a touch is detected
both r working fine individually but when simultaneously occurring anyone of the animation slows down .its because in the given time unit is performing only one action
for NSTimer im using time interval of 0.01s
How do make this scenario work without slowing down any animation?
NSTimer is not the tool for performing animations. Core Animation is what you want for almost all animation problems on iPhone. NSTimer is not a real-time timer. It has not guarantees on when it fires. It's an excellent tool for many problems; animation is not one of them.
Touches refusing to respond usually indicates a stressed CPU. Animating 100 times a second is probably the cause of that.
I would also stop the timer from firing while the touch animation was in progress. You can call [timer invalidate] to stop the timer from firing. Maybe that would do the trick? Time interval of 0.05 would be much more reasonable, though - and may solve the problem entirely! Good luck!