So I am fooling around with some apps and I am trying to get a game timer to be in milliseconds instead of just the normal 1,2,3. I want it to be in a format similar to this 1.000,2.000. Or even 1.00,2.00.
Here is my code:
(void)startTimer {
if (count == 30) {
// 3
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target: self
selector:#selector(ElapsedTime)
userInfo:nil
repeats:YES];
Record the start time with
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
Any time you want to see how much time has elapsed, use:
NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] -
start;
elapsed will be in seconds. If you want milliseconds, subtract the whole number to get the decimal part. Multiply the decimal part by 1000 to get milliseconds.
If you want your time to change more often than once a second, make the time interval of your timer less than 1.0 seconds. Note that timers aren't accurate for more than about 1/50 of a second however.
Related
I am working on a project in which i need to get location updates in all states of application at a particular time interval. I have used NSTimer to call locationUpdate method at a particular time interval in background and foreground. But Timer is only works in background for 1-2 hours if interval time is around 30 secs, When i increase interval to 300 secs timer does not work in background even for 10 mins. Please tell me - Is there any way to run timer in background for at least one day with time interval around 300 secs or get location update at particular time interval in all states????
Thanks in Advance!!
Is there a reason you prefer NSTimer over background location updates?
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// Add the new locations to the hike
[self.hike addLocations:locations];
// Defer updates until the user hikes a certain distance
// or when a certain amount of time has passed.
if (!self.deferringUpdates) {
CLLocationDistance distance = self.hike.goal - self.hike.distance;
NSTimeInterval time = [self.nextAudible timeIntervalSinceNow];
[locationManager allowDeferredLocationUpdatesUntilTraveled:distance
timeout:time];
self.deferringUpdates = YES;
}
}
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html
I have a driving application when I start the trip I have to collect location details along with date and time in hours, minutes, seconds and milliseconds for every second and accelerometer details along with date and time in hours, minutes, seconds and milliseconds for every 0.25 seconds and when the trip is stopped recording should be stopped. I have taken a timer for location details with interval 1 second
self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(locationTimerFired:)
userInfo:nil
repeats:YES];
- (void) locationTimerFired:(NSTimer *)timer
{
CLLocation *newLocation = locationManager.location;
[self updateLocation:newLocation andSpeed:newLocation.speed];
[defaultCenter postNotificationName:LocationChangedNotification
object:nil
userInfo: userInfo];
}
in the received LocationChangedNotification I am recording the location and time as mentioned for every second.
This is sometime fine when the application is in foreground but when the application is in background timer timer fires interval is not accurate it fires sometimes for 2 seconds, for 3 seconds and some times 2 times a second why this is happening?
Please suggest. Also I have registered for location updates in background mode in info.plist.
I have taken a timer for accelerometer details with interval 0.25 second as follows
self.accelTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
Thanks.
Don't use timers. Instead, configure the location manager to give you callbacks at your desired accuracy (presumably maximum) and use the callback to save the location and speed with the current time.
Then, when you come to use / display the data you can filter / extrapolate to get the location data at your desired time interval.
The timers don't work in the background because they aren't designed to. Location callbacks are designed to...
I want to create a NSTimer that runs for lets say 10 minutes. I then want to write a while loop aftewards delaying 10 minutes of time before the line afterwards is executed. For example.
NSTimer * countDown = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector userInfo:nil repeats:NO];
while (countDown == stil running (hasnt reached 10 minute mark) ){
// kill 10 minutes of time
}//when 10 minutes is up
execute next line of code
First
The timeInterval parameter of scheduledTimerWithTimeInterval: method is in seconds. If you want in minutes, don't forget to multiply by 60.
Second
Why would you want to wait the countDown with a while like that? Just call the lines you want to execute 10 minutes later in the selector that NSTimer fires.
NSTimer * countDown = [NSTimer
scheduledTimerWithTimeInterval:(10.0 * 60)
target:self
selector:#selector(tenMinutesLater)
userInfo:nil
repeats:NO];
And then
-(void)tenMinutesLater
{
//put some code here. This will be executed 10 minutes later the NSTimer was initialized
}
Instead of a while loop, just make a new timer inside of a method called by your first timer.
NSTimer *countDown = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(startSecondTimer) userInfo:nil repeats:NO];
- (void)startSecondTimer
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(doSomething) userInfo:nil repeats:NO];
}
PS, the time interval is in seconds - 10 mins = 600 seconds, not 10.
How can I create a simple timer which will give me simply the Time in millisecond. Something like the following...
Timer *timer = [Timer alloc] init];
[timer start]; //Which will start from Zero
[timer pause]; //Which will Pause timer
[timer stop]; //Which will stop the timer
[timer getCurrentTime]; //Which will give me time elapsed in millisecond since [timer start] is called
NSTimer doesn't seem to Work as it provides more functionality but not this. Can I get this functionality using NSTimer? What would be the best way to achieve this?
NOTE: I don't want to call any function at any specific time period but just want the milliseconds since the timer started.
Why do you need a timer for that?
Just store the start time and subtract it from the current time when you stop measuring:
static NSTimeInterval startTime;
static BOOL isRunning;
- (IBAction)toggle:(id)sender
{
if(!isRunning)
{
startTime = [NSDate timeIntervalSinceReferenceDate];
isRunning = YES;
}
else
{
NSLog(#"%f", ([NSDate timeIntervalSinceReferenceDate] - startTime) * 1000.0);
isRunning = NO;
}
}
Actually i Was looking for this but somehow i missed this question
How Do I write a Timer in Objective-C?
Is this the right way to check AVAudioPlayer's current playback time.
[audioPlayer play];
float seconds = audioPlayer.currentTime;
float seconds = CMTimeGetSeconds(duration);
How I can code after
[audioPlayer play];
that when currentTime is equal to 11 seconds then
[self performSelector:#selector(firstview) withObject:nil];
and after firstview when currentTime is equal to 23 seconds
[self performSelector:#selector(secondview) withObject:nil];
You could set up a NSTimer to check the time periodically. You would need a method like:
- (void) checkPlaybackTime:(NSTimer *)theTimer
{
float seconds = audioPlayer.currentTime;
if (seconds => 10.5 && seconds < 11.5) {
// do something
} else if (seconds >= 22.5 && seconds < 23.5) {
// do something else
}
}
Then set up the NSTimer object to call this method every second (or whatever interval you like):
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(checkPlaybackTime:)
userInfo:nil
repeats:YES];
Check the NSTimer documentation for more details. You do need to stop (invalidate) the timer at the right time when you're through with it:
https://developer.apple.com/documentation/foundation/nstimer
EDIT: seconds will probably not be exactly 11 or 23; you'll have to fiddle with the granularity.