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.
Related
On my story board, I have set up a timer (which updates every 0.01 seconds) and a UIMapView which displays the user's location when the view controller is loaded. However, when this screen loads, the timer lags for a bit until the map is fully loaded. I assume this is because the loading of the map is a blocking function performed on the main thread.
Is there any way to load the map in the background and display it after it has fully loaded while still keeping the timer running?
Probably not. A map view is a view object and view objects are not thread safe.
Note that Apple's docs on NSTimer say:
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.
50 ms is .02 seconds. 100 ms is .04. Thus you are not likely to get the accuracy you need.
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 have developed a test for iPod/iPhone (with MonoTouch if that is relevant) that measures reaction time. But I need to take into consideration the time between touching the screen and actual triggering of the button event. Is there any documentation of that?
It's already very hard to almost impossible to get predictable interrupt latency on real time operating systems.
But on the iPhone? Imho impossible. A capacitive touchscreen is not optimal to get results that are exactly the same for each body and location. And if mail.app decides to poll for emails just at the moment you'll touch the screen there will be a bigger delay.
But to make one thing clear, we are speaking about some micro seconds or even less than that.
If you want accurate results you shouldn't use an iPhone. But I guess your app will be some kind of game, so nobody cares if your result is 0.01 seconds off. But I wouldn't show results as 0.381829191 seconds, that fakes accuracy you'll never get on any smartphone.
What is the lowest reaction time you got in your app?
The time between an actual touch and the system registering it will be negligable.
One key thing: if you are detecting the press using touch events like touchUpInside, consider using the touchesDownInside event because touchesUpInside, will not fire until the user's finger leaves the screen.
Making a simple card game, and it it should ok when the user is in control since he will push a button. It will call my method assigned to that button and logic will be performed and screen updated.
But when the players turn ends, and i want the AI to run everything for a few seconds, update the screen with its decisions etc. Handle some logic, call some animation before handing the control back to the user.
Is there a method i can override in my Controller class that which is a subclass of NSObject that gets called every loop or at least 5-10times a second? Or how is it you guys handle this?
Thanks
-Code
It doesn't seem like you want a background thread at all (at least not one you make) or a timer.
What you really want to to is visually animate the AI actions, to that end look at the CoreAnimation stuff, to define animations for AI actions and then play them. You can specify a time period an animation is to take.
Look at this project for examples of animation from the simple to the complex:
http://github.com/neror/CA360
Just create a an NSTimer that calls a tick method at whatever frequency you desire. But keep in mind that NSTimer is not guaranteed to be precise, so in order to avoid gradually accumulating errors, you might want to check how much time has actually passed (e.g. if the timer fires an average of 10 ms late over 500 ticks, code that depends on precise timing will be five seconds off).
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!