How to change animations on one screen View by timer - ios

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

Related

Progress bar not animating properly after pausing and resetting mid animation

I have a progress bar that animates from 0-1.0 continuously. I set it to 0 if the user pauses the app. The problem is if they resume quickly the progress bar doesn't go to 1.0 like it should.
I know (well am pretty sure) it's an issue with a doubling of the animation.
I have the progress bar going from 0 - 1 in 15 seconds
I pause
I set the progress bar to 0 and remove all animations
I resume right away
The progress no longer fills the bar on subsequent animations
If I wait > 15s to resume there is no issue.
Here is the code that animates my progress bar.
self.progressBar.layer.removeAllAnimations()
self.progressBar.progress = 0.0
self.progressBar.layoutIfNeeded()
self.progressBar.layer.removeAllAnimations()
self.progressBar.progress = 1.0
UIView.animate(withDuration: time) {
self.progressBar.layoutIfNeeded()
}
I go from 0 - 1.0 progress continuously and I was having issues with the completion block not going to 0 in time. (That's why it resets before it animates)
If the user pauses it:
self.progressBar.progress = 0.0
self.progressBar.layoutIfNeeded()
progressBar.layer.removeAllAnimations()
Then when I go to resume the progress bar doesn't get to 1.0 progress. It continuously increases the amount each time the func is run until after about 5 cycles.
While researching this issue I came across a SO post about using CATransaction to reset the progress bar to 0 upon completion but that caused the issue with the second, fourth, etc cycles not animating because of the speed I call it.
This issue presents itself when you try to set progress to both 0 and 1 at the same time. However, I'm not doing that here as far as I can tell. It works 100% except in this pause/resume scenario.
I am 99% sure its a doubling of progress animations but I'm not sure how that's possible when I am removing the animations before animating it.
Note: Using progressBar.setProgress doesn't change anything.
Thanks in advance
To remove all animations from progressView you can use this approach:
for (CALayer *layer in self.progressView.layer.sublayers) {
[layer removeAllAnimations];
}

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.

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.

UIScrollView animation problems

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.

Resources