how to implement a timer (including milliseconds) into an app - ios

So I am creating a basic game for iOS (using exclusively objective-c) Xcode and have looked all over but cannot seem to find any tips for a starting point. I just want to time the player while the game is playing and have the scene all set up but can't figure out how to generate a timer just to display the time as the player plays and then pass it to my game over scene when the game finishes.
any tips or help what so ever would be greatly appreciated.

There are 2 parts to this. One part is to have a repeating event to display new info (elapsed time) to the user. For that NSTimer is perfect. See the comments from trojanfoe and tnev for info on NSTimer.
The second part is calculating the amount of time that's elapsed.
For that I recommend the following:
Create an instance variable of type NSTimeInterval (a double) in your view controller class:
#property (nonatomic, assign) NSTimeInterval startTime;
In your game start method, save the current time interval:
-(void) gameSart
{
self.startTime = [NSDate timeIntervalSinceReferenceDate];
//Also start an NSTimer to display elapsed time.
}
Then code to calculate elapsed time (or total time):
NSTimeInterval elapsed =
[NSDate timeIntervalSinceReferenceDate] - self.startTime; //in seconds
//Display elapsed time to a label, perhaps formatted as minutes/seconds.

Related

Get remaining time of Timer in Dart

How to get the remaining time in seconds of Timer from 'dart:async' in Dart?
I have a countdown timer and at some point I want to get the remaining time in seconds. How do I do this in dart?
_endTimer = Timer(Duration(seconds: totalRemainingTimeInSeconds), () {
_end();
});
You cannot get that from a Timer.
What you can do is to start a Stopwatch at the same time as the timer.
If you need to do things along the way, they can check the stopwatch to see the duration so far. To update a countdown timer, I'd probably use a combination of Stopwatch to count time and Timer.periodic to update the UI occasionally, and either have the periodic timer check whether the countdown is over, or have a single Timer to react on timeout.

Accurately delaying time between KIF waitForViewWithAccessibilityLabel and tapViewWithAccessibilityLabel

Is there a way to accurately (to thousands of a second) delay the time between waiting for a view to appear waitForViewWithAccessibilityLabel and then tapping another view tapViewWithAccessibilityLabel in KIF. In my app code I set to DateTime objects on view appear and tap but this time difference does not match the delay I put in KIF.
I have tried waitForTimeInterval and also
while(true) {
NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:dateStart];
if(time > 1.678)
break;
}
but those both have errors of about 0.15 seconds. I assume that this error comes from the waitForViewWithAccessibilityLabel looking for the view to appear. Is there any way to set the start time of a the timer at the time the view is actually found? Or any other suggestions on solving this problem?
Cheers,
Mo

Pause CFTimeInterval Timer?

In my game I have a timer in my update loop that updates a label based upon the interval returned in this line:
elapsedTime = 90 - (CFAbsoluteTimeGetCurrent() - startTime);
Now 90 is the time we tick down from. I set startTime like so:
startTime = CFAbsoluteTimeGetCurrent();
Now what I am aiming to do is "pause" this timer when I go to my pause menu. However I just cannot wrap my head around the logistics of it.
When I pause should I set startTime to 0? If so, what would I do during the resume method?
Basically if the user pauses the game with 85 seconds to go, I want the user to come back from the pause screen with 85 seconds to go still. How would I achieve this?
I got my answer. I just did what I was doing now but I included another timer for the paused screen and I just add it up continually when paused and then I incorporate that timer into my game timer and it worked out.
You could store the amount of time, that passed since the timer started.
With NSDate you can store the startTime of the timer and than use -timeIntervalSinceNow to retrieve the time that passed.

UIDatePicker with minimum time

I'm developing for ios and I'm in the making of a UITextField containing UIDatePicker as its inputView.
My problem is as follows: I have set the minimum time to now --> [NSDate date] and that's because I don't want the user to enter a time that is before now.
This works fine, but if the user stays in the ViewController for when a time passes in the phone, I get a wrong time (1 minute before now).
Is there a way to listen to the hardware clock, and sync with the phone's clock? like listen to the iPhone's (hopefully) notification for changing the minute?I don't want the user to see wrong time, I want the text field to change if the time passes.
I thought of an implementation and I'd like to hear another perspective since mine is not easy at all...
My implementation:
1. run a delayed GCD async for the seconds left in the current minute, and when that reaches, listen for every 60 seconds... but this doesn't seems like the right idea, since if the user leaves the ViewController I might get a leak or user puts the app in the background, it'll be frozen and continue from the wrong place (According to GCD documentation).
Any suggestion will be very appreciated.
Rather than using GDC, take a similar approach but with NSTimer. invalidate the timer when the view is hidden (viewDidDisappear:) and observe app notifications for going to the background and returning to the foreground to reset your timer.
You need to calculate the end of the next minute, this becomes the first fire date (fireDate). Once you have that:
NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate interval:60 target:self selector:#selectior(...) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode: NSDefaultRunLoopMode];
Configure your date picker behind a getter or a method of it's own and do something like this, in the UITextField delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UIDatePicker *datePicker = self.datePicker;
datePicker.minimumDate = [NSDate date];
textField.inputView = datePicker;
return YES;
}

NSTimer start delay

I have an UIButton that triggers a NSTimer with 1 sec interval, it calls a selector a countdown, this countdown is shown in the screen. We have detected that the NSTimer does not start immediately, it costs one second aprox. to begin. Is there anyway to force NSTimer to start immediately?
Thanks!
NSTimer has a fire method. call it after initialization.
check the documentation here https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/occ/instm/NSTimer/fire

Resources