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)
Related
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];
}
I'm using an NSTimer to call a method every second, but it doesn't work. I can call it manually using [refreshTimer fire]; but otherwise it doesn't work. Any ideas why?
Here is where I initialize the timer:
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(sendRefreshPacket:) userInfo:nil repeats:YES];
And then the method:
- (void)sendRefreshPacket: (NSTimer*) timer
{
NSLog(#"Test");
}
Try this:
dispatch_async(dispatch_get_main_queue(), ^{
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(sendRefreshPacket:) userInfo:nil repeats:YES];
});
I have strange issue with timer. timer is updating well within application. i am showing the code.
// Start Timer
#pragma mark- Timer
- (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES];
}
// Update Timer
- (void)updateTimer:(NSTimer *)theTimer
{
NSLog(#"called");
}
Problem: I have a textview when i scroll text within the
textview the updateTimer method stops calling and when I stop
scrolling then it starts to update timer.
Now what to do to continue calling the update timer method?
For do fix this issue you need to add your NSTimer to the mainRunLoop. Such like,
- (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
You can do this way also,
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.001
target:self
selector:#selector(updateTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer
forMode:NSRunLoopCommonModes];
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];
}
}
I have coded a timer using an NSTimer that updates a label. The problem is that in the same viewcontroller I have a uitableview and when I scroll it down, the timer doesn't update its value so the user can "cheat" to stop the timer.
I think this could be easy to fix with a serial queue with CGD but I don't figure out how to do it.
Thanks in advance,
Juan
First of all keep in mind that you cannot perform UI changes in any other thread than the main thread. Having said that, you need the NSTimer to fire in the main queue, otherwise the program will crash when changing the UILabel. Have a look at this links http://bynomial.com/blog/?p=67 and this one http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html
To my knowledge, if you schedule the timer in the for the NSRunLoopCommonModes it will ignore event updates and fire the timer jsut how you want it:
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:#selector(timerDidTick:)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void) timerDidTick:(NSTimer*) theTimer{
[[self myLabel] setText:#"Timer ticked!"];
}
I have faced same problem if u run timer in main thread than when you scroll tableview its stop timer too. Solution is that you run timer in background and update GUI in main thread
UIApplication *app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//run function methodRunAfterBackground
updateTimer1=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateGUIAudioPlayer) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:updateTimer1 forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
-(void)updateGUIAudioPlayer {
dispatch_async(dispatch_get_main_queue(), ^{
self.label.text = #"your text";
});
//but if you want to stop timer by himself than use main thread too like that
dispatch_async(dispatch_get_main_queue(), ^{
[updateTimer1 invalidate];
});}
-(void) timerDidTick:(NSTimer*) theTimer{
_currentNumber =_currentNumber+1;
//check if current number is bigger than the contents of the word array
if (_currentNumber <=wordArray.count-1) {
NSLog(#"_currentNumber #%i wordArray #%i",_currentNumber,wordArray.count);
_RandomLoadingText.text = #"Timer" ;
[self changeTextInRandomLoadingLabel:_currentNumber];
}
else if (_currentNumber >=wordArray.count-1)
{
NSLog(#"reached end of array");
[timer invalidate];
}
}
-(void)changeTextInRandomLoadingLabel:(int)myInt
{
_RandomLoadingText.text = [wordArray objectAtIndex:myInt];
}
//---------
view did load
_RandomLoadingText.text = #"Test";
_currentNumber =0;
timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:#selector(timerDidTick:)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
To update the UI you need to use OperationQueue.main. Supposing you have a UILabelnamed timeLabel:
var timeInSeconds = 0
timer = Timer(timeInterval: 1, repeats: true, block: { timer in
timeInSeconds += 1
OperationQueue.main.addOperation {
self.timeLabel.text = String(timeInSeconds)
}
})
RunLoop.main.add(timer, forMode: .commonModes)