Take current Countdown Timer value - ios

I am using a UIDatePicker in the countdown timer mode. I want to take the current set time on the uidatepicker in seconds and put it in an integer value? How is this done?

The UIDatePicker has a property called countDownDuration so you should be able to use
int seconds = (int)datePicker.countDownDuration;
Edit: to address concern in the comment, make sure to manually set either the countDownDuration or the time of the datePicker in order to get a "whole-minute" value. For instance, in viewDidLoad you could set:
datePicker.countDownDuration = 60.0f;
and then the default-selected time duration in your countdown would be exactly 0 hours, 1 minute. Otherwise, by default the date picker will use the current date/time and setting your countdown timer could result in countDownDuration being up to +59 seconds (e.g. 1 minute could ready anywhere from 60-119, 2 minutes from 120-179, etc) based on the time when you run your app.

Related

Date picker with minutes and seconds

I'm new to Swift and fairly new to programming in general. I'm making a basic countdown timer app and want to use a date picker to select the duration of the countdown. I have a label which is a concatenation of a timeMin variable and a timeSec variable, seperated by a ":" string.
When the date picker's mode is set to countdown timer, it can only do hours and minutes. Is there any way I could change this to minutes and seconds? The text displayed on the date picker should be minutes and seconds too, rather than hours and minutes.
So if my label starts off as 02:00, using the date picker and selecting 4 in minutes and 35 in seconds should simply result in the label changing to 04:35.
Thanks for taking the time to help.
This is not possible with UIDatePicker, If you want to implement this, then you have to create custom datepicker by using UIPickerView. You have to take 2 components in UIPickerView, one for minutes and one for seconds. And supply the values(with appropriate interval) from titleForRow: ... method of UIPickerViewDatasource.

Perform an action after sprites have touched for 2 seconds in swift?

How do I perform an action after sprites have been in contact for a certain time rather than when they collide right away?
I assume you have some frame update method that gets called every frame, and also you can calculate how much time (seconds) elapsed since then. Then, you can do the following:
Setup a flag variable named contactInProgress, set to false.
Setup a counter variable named contactDuration, set it to 0.0.
When you detect collision, set contactInProgress to true and contactDuration to 0.0.
When you no longer detect collision, set contactInProgress back to false and reset contactDuration to 0.0.
In the frame update method, check if contactInProgress is true. If so, update the value of contactDuration by adding the time elapsed since the last frame. Finally, check whether contactDuration is greater-than-or-equal-to 2.0. If so, do your thing.

Objective C: how to get an integer from uitextfield to work as a timer

I'm creating my first app with Objective C, one function with let the user to provide an amount (in numbers) with 2 decimal places into a UITextField. I would like that to be processed using a UIButton to appear in a UILabel which runs according to a timer.
I.E. if the user inputs 5.60 into the UITextField, when the UIButton is pressed, this amount should transfer to the UILabel which acts as timer and counts to 5.60 every 60 seconds.
The counter should go up by 0.01 to reach 5.60 in 60 seconds.
This is all on one ViewController.
Thanks in advance if anyone sees this and has the time to help!

Resume NSTimer when view loads back

So I have a view that has a timer on it. So when you press "start" the NSTimer starts updating the UILabel with the time elapsed. Now when you press back to previous view, I store the current duration of the timer and when I load this view back I want to start the NSTimer again but this time I want to display the time that has passed.
So for example,
Start timer: 0:0:0
Pressed back at: 0:0:10
When I come back to the view after 30 seconds, right now NSTimer starts back from: 0:0:10 when it should be at 0:0:40
This is how I am calculating the resume time:
NSDate *resumeDate = [today dateByAddingTimeInterval:interval];
Any idea on how to get the correct timestamp?
An NSTimer cannot be "started again". Once you start the timer all you can do is let it run or stop it.
You have to create a new NSTimer object with the new timestamp.
As for how to add to a timestamp, the NSTimeInterval data type is an alias for a standard C double value. So you can just do this:
interval += 4.2 // add 4.2 seconds to the interval
You can not pause a timer but to stop it. So if you want to "Resume NSTimer", you can only create a new timer the same as your previous timer and let it run.

timer.invalidate() not working on same view in Swift

I am using Swift to develop an app that uses timer so i am using this code for timer
var timer = NSTimer()
timer=NSTimer.scheduledTimerWithTimeInterval(1.0,target:self,selector:Selector("updateTimeLabel"), userInfo:nil , repeats: true)
my problem is this when this game complete a view appears as subview of current view and the timer invalidates using
timer.invalidate()
and on this subview there is a button to start a new game when this button is pressed a new game is started but the timer starts from the time it stopped.
suppose the game finished in 00:10 time so the subview appears and if we start new game then the time will start from 00:11. This works fine when user moves to another view and then comes back to the same view for game play, in this case the timer starts from 00:00
pls tell where i am wrong
THANKS IN ADVANCE.
You should reset your timeSec and timeMin variables when he decides to start a new game.
In other words, whenever that subview with the button is removed from its superview, reset it.
Or, do it into that button action.
reset your sec and minute variable that are updating the label in your updateTimeLabel method. this would do
Your timer is only used to fire the method that will update the time label.
You need to reset the variable that holds the elapsed time to zero when the game finishes. Probably timeSec in your case, which is hold by your GameScreen class.

Resources