ios scheduledTimerWithTimeInterval for amount of time - ios

I want to use scheduledTimerWithTimeInterval to do a periodic task for certain amount of time lets say one hour. but How do I implement on my code. Here is my code:
timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
target: self
selector: #selector(doSomething:)
userInfo: nil
repeats: YES];

Lets assume for a minute .
Here is simple scenario ,
int remainingCounts;
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:#selector(countDown) userInfo:nil repeats:YES];
remainingCounts = 60; // int remainingCounts ;...
Method being called for every sec.
-(void)countDown {
NSLog(#"%d", remainingCounts);
// Do Your stuff......
if (--remainingCounts == 0) {
//[self dismissViewControllerAnimated:YES completion:nil];
[timer invalidate];
}
}

Related

How to change NSTimer's timeInterval dynamically?

The timeInterval of NSTimer is readOnly,we can not change it directly.Is there any way to do it?
Becasue the timeInterval of NSTimer is readOnly so we can not change it directly.
The way to change timeInterval is redefine the timer,such as:
- (void)changeTimeIntervalWithInterval:(NSInteger)timeInterval{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:#selector(printHH) userInfo:nil repeats:YES];
}
}

NSTimer stops my image from moving

When I hold down on a button my image will move to a direction.
The game objective is that they are going to move image to hit another image, and to do that I'm using an NSTimer.
Then they also have another NSTimer because they only have 15 seconds on them to hit the other image. If they don't the game will [self gameover] but, the NSTimer that counts the 15 seconds is started and I move the image the image moves a bit and then it goes back to center again.
When I delete the countdown timer, the image does not go back to center after I stop pressing the button but it stops on its current position.
And that's what I want it to do while I'm using the countdown timer as well.
Code:
-(void)goLeft
{
ball.center = CGPointMake(ball.center.x -5, ball.center.y);
}
-(IBAction)left
{
goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
if (goLeft == nil) goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
}
-(IBAction)stopLeft
{
[goLeft invalidate];
goLeft = nil;
}
//And heres the timer :
-(IBAction)doCountdown: (id)sender;{
if (countdownTimer)
return;
remainingTicks = 25;
[self updateLabel];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: #selector(handleTimerTick) userInfo: nil repeats: YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void)updateLabel
{
theLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}

Slow down NSTimer with a for loop in Objective-C

I have a button that when pressed generates random numbers triggered by NSTimer at a certain frequency, and when released I would like it to keep generating them, just more slower and slower until it stops.
- (void)buttonPressed {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(generateNumber) userInfo:nil repeats:YES];
}
- (void)buttonReleased {
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
for (float i=0.20; i<1; i += 0.1) {
_timer = [NSTimer scheduledTimerWithTimeInterval:i target:self selector:#selector(generateNumber) userInfo:nil repeats:NO];
}
}
It works fine while the button is pressed, but once I release it actually accelerate and then stops all in a sudden. Any idea why it wouldn't be working or any alternative way I can reach the wanted result?
Once your button is released, generation accelerates because at that moment you schedule 8 timers with intervals 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 and 0.9 starting at the time of button release, which means each will execute 0.1s after the other, which is a smaller interval than your original timer (0.2s).
This is an alternative
#interface ViewController ()
#property (nonatomic, retain) NSTimer *timer;
#property (nonatomic) float curInterval;
#end
#implementation ViewController
- (IBAction)buttonDown {
self.curInterval = 0.1;
[self updatePressed];
}
- (void)updatePressed {
[self generateNumber];
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.curInterval target:self selector:#selector(updatePressed) userInfo:nil repeats:NO];
}
- (IBAction)buttonUp {
[self.timer invalidate];
[self updateUp];
}
- (void)updateUp {
[self generateNumber];
self.curInterval += 0.1;
if (self.curInterval < 1.0f) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.curInterval target:self selector:#selector(updateUp) userInfo:nil repeats:NO];
}
}
#end
In Button release method you are using for loop which keeps on executing untill it reaches interval of 1. What you need to do is take a global float value and initialize it to 0.20 at start. On button pressed pass that value. On button release increment the global variable and pass that value to NSTimer instead of for loop
float numTimer = 0.20f;
- (void)buttonPressed {
_timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:#selector(generateNumber) userInfo:nil repeats:YES];
}
- (void)buttonReleased {
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
numTimer = numTimer + 0.1f;
_timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:#selector(reGenerateNumber) userInfo:nil repeats:NO]; }
}
-(void) reGenerateNumber {
[self generateNumber];
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
numTimer = numTimer + 0.1f;
_timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:#selector(reGenerateNumber) userInfo:nil repeats:NO];
}

How to release NSTimer? or stop timer. Xcode 5

After the end of our game the NSTimer does not stop.
-(void)StartGame{
Countdown = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(TimeDecrease) userInfo:nil repeats:YES];
}
-(void)TimeDecrease{
StartTime = StartTime - 1;
Timer.text = [NSString stringWithFormat:#"%d",StartTime];
if(StartTime == 0){
[self EndGame];
}
}
We tried [Countdown release] and disabled ARC but it throws an error.
We are trying to accomplish a timer that resets to 15 at the beginning of every new game.
Try:
[Countdown invalidate];
Countdown = nil;

iOS: UILabel countdown hangs on other UI operation

I'm implementing a countdown with its value displayed on a UILabel, but have run into a problem. Here's the simplified code:
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(countdown) userInfo:nil repeats:YES];
- (void)countdown {
self.countdownLabel.text = [NSString stringWithFormat:#"%i",[self.countdownLabel.text intValue]-1];
// Handle time out
if ([self.countdownLabel.text intValue] == 0) {
[self.countdownTimer invalidate];
self.countdownTimer = nil;
}
}
It works fine, but if I do other UI operations in the viewcontroller, like scroll a scrollview, the timer hangs as the scrollview scrolls and then boosts up to make up for the idle moments.
I tried dispatching the updating of the label to a background queue which of course didn't work.
dispatch_queue_t bgQ = dispatch_queue_create("bgQ", 0);
dispatch_async(bgQ, ^{
self.countdownLabel.text = [NSString stringWithFormat:#"%i",[self.countdownLabel.text intValue]-1];
});
What would be the solution here?
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(countdown) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.countdownTimer forMode:NSRunLoopCommonModes];
Remember that in your countdown method you need an escape to invalidate your countdownTimer.
The timer will start without fire instruction when the
[[NSRunLoop mainRunLoop] addTimer:self.countdownTimer forMode:NSRunLoopCommonModes];
line is executed.
ABSOLUTELY no dispatch async for UI changes.
Hope this helps.
Swift 3.0 syntax
var timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true);
RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)

Resources