Resume NSTimer when view loads back - ios

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.

Related

How to change animations on one screen View by timer

I have several animations that I need to change to one timer on a viev. There are 2 buttons. Start and Stop and 2 Label (total time and time of animation). When you press the start, the animation starts and the timer is counted, then after 30 seconds the animation changes and so on. And when you click stop, the animation and time stop.
An example on this video: https://drive.google.com/open?id=1ey8lGmA1icPlojTMQlF3Auosd2IdEKSs

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.

Take current Countdown Timer value

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.

Do an action if a button is pressed for a select amount of time

I'm making a little game for myself, and I want a void to fire for every 0.1 seconds that the UIButton is pressed down (e.g., if the button is pressed for 0.7 seconds, the void will fire 7 times within that timeframe).
Is this possisble with a UIButton or do I need to create subviews that will detect a touch? If I use a subview, how do I make the method fire for every 0.1 seconds that the UIButton is pressed for?
You could add a selector for UIControlEventTouchDown that starts an NSTimer ticking at a 0.1 second interval and then stop it on UIControlEventTouchUpInside and/or UIControlEventTouchUpOutside. If you are concerned about stopping it while the user's finger is not over the control, you can hook up a cancel event to UIControlEventTouchDragExit as well.
The UIControl method to use is addTarget:action:forControlEvents:. Note that the last argument is a bitmask, so you can hook up all the cancel/stop scenarios in one line of code.

Hide a button if not interacted with in a certain amount of time

I am looking to implement something similar in behavior to the zoomControl on android, where buttons appear, and stay visible if being interacted with, but if they haven't been interacted with for a certain period of time, they fade away.
General logic would be something like this:
Show Button: begin some sort of timer/event for the time I want it to display
if interaction with button occurs reset the timer/event to new time amount
if timer/event is hit, hide the button.
I could run some performSelector afterDelay, to a method that would hide the button, with every interaction, and have the hide method button called by the performSelector check some count/flag that would be incremented with each interaction.. and this would work, but it seems rather inelegant like:
show button : increment count :performSelector Hide after 5 seconds
button action : increment count : perform selector Hide after 5 seconds
Hide : decrement count if count !=0 do nothing, otherwise hide button
Is there a better way? This just feels kludgy to me.
Make an NSTimer with
myButtonHideTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(hideButton:) userInfo:nil repeats:NO];
Then if you get an interaction before the timer fires call
[myButtonHideTimer invalidate];//This stops the event from triggering
And then recreate the timer to reset the trigger time
I would go the performSelector:withObject:afterDelay: route because you don't have to handle a dedicated timer object. Don't know what you need the counter for, though.
Call performSelector:withObject:afterDelay:.
When user interaction occurs, call cancelPreviousPerformRequestsWithTarget:selector:object:. Then call performSelector:withObject:afterDelay: again.

Resources