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.
Related
My app have circle animation from the framework connected to the timer. When the timer is running, the animation starts. If the app goes into the background, the animation stops. When we return to the app, the animation continues from the same place, when did stopped. How do I get the animation to recalculate and start from where the timer is now? I attached screenshot. All animation methods are concentrated in one file
I created a new method in the framework, where I pass the remaining time. The method changes the value in the timer. Thanks guys
public func updateTime(time: Int) {
elapsedTime = TimeInterval(time)
delegate?.timerDidResume?(sender: self)
}
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
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.
I'm using a horizontal UIScrollView that displays photos on the first half of the screen. (Imagine a CGRect (0,0,320,120). The first scrollView is embedded in a second scrollview, which takes all the screen. When I scroll down the page (thus the second scrollView), the first scrollview stop being animated. I programmed a NSTimer to change photo every 3 seconds in the first UIScrollView, but while I'm scrolling the second scrollView, it seems like the animations are being queued. When I release my finger of the screen, there are few photo transitions (i.e: 2 changes if I scrolled without stoping for 6 seconds). In short: how can I make use of blocks (or something else) to continue my animations while I'm scrolling the second scrollView?
My NSTimer (in viewDidLoad):
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(changePage:) userInfo:nil repeats:YES];
Your iOS app has a run loop that continually checks for things like user input. If it is busy detecting touch events, things that are scheduled on that run loop will not get performed. (Similarly, if you block the main thread with e.g. a big server request, you won't get touch events during that time).
Here's how you can get the NSTimer on the right run loop:
First, use timerWithTimeInterval:target:selector:userInfo:repeats: to create a timer not scheduled on a run loop.
Second, use - (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode to schedule the timer on a run loop. For the mode argument (the run loop type), use NSRunLoopCommonModes to allow the timer to fire without having to wait on the main run loop.
Note that this same effect exists for e.g. performSelectorAfterDelay if done in the main run loop. It also applies to animation completion blocks. I'm not sure if there's an easy way around the animation completion block problem besides using CoreAnimation.
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.