Questions tagged [nstimer]

An Objective C class for creating timer objects.

In Objective C (), you use the NSTimer class to create timer objects or, more simply, timers. A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. For example, you could create an NSTimer object that sends a message to a window, telling it to update itself after a certain time interval.

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

NSTimer is “toll-free bridged” with its Core Foundation () counterpart, CFRunLoopTimerRef. See “Toll-Free Bridging” for more information on toll-free bridging.

Additional examples for the NSTimer object can be found here.

2624 questions
7
votes
2 answers

How to animate incrementing number in UILabel in swift

I have a label showing a number and I want to change it to a higher number, however, I'd like to add a bit of flare to it. I'd like to have the number increment up to the higher number with an ease-in-out curve so it speeds up then slows down.…
7
votes
1 answer

Animation stops when scrolling in UIScrollView

So I am trying to make a game where the user must scroll down as fast as they can in order to escape a "block" that grows infinitely bigger. Here is my problem: I am using a UI ScrollView as my mechanism for scrolling with a normal UI View as a…
woakley5
  • 304
  • 3
  • 17
7
votes
3 answers

Unrecognized selector sent to instance NSTimer Swift

I'm trying to developing an app that includes a simple Stopwatch feature. I'm using Xcode 6 and the Swift language. Here is the code in FirstViewController @IBAction func Stopwatch (Sender:UIButton) { var startTime = NSTimeInterval() func…
That_one_guy
  • 71
  • 1
  • 2
7
votes
2 answers

NSTimer with a menu bar app

I'm working on a simple timer app, and I've created a NSStatusItem with a menu and I have some NSTextField labels that updates the timer labels (http://cld.ly/e81dqm) but when I click on the status item the NSTimer stops (and stops updating the…
nanochrome
  • 707
  • 2
  • 12
  • 26
7
votes
3 answers

method not called with dispatch_async and repeating NSTimer

I am developing an app where i want to call method in separate queue using dispatch_async. I want to call that method repeatedly after certain interval of time. But the method is not getting called. I don't know whats wrong. Here is my…
user2185354
  • 519
  • 7
  • 23
7
votes
4 answers

Accuracy of NSTimer

I am trying to use NSTimer to create a Stop-watch style timer that increments every 0.1 seconds, but it seems to be running too fast sometimes .. This is how I've done it: Timer =[NSTimer scheduledTimerWithTimeInterval: 0.1 target:self…
user2308343
  • 107
  • 2
  • 7
7
votes
4 answers

How to display time in seconds in Cocoa efficiently?

I want to display the time (including seconds) in an application in a text field, pretty simple. What I currently do is create a NSTimer that runs every 0.1 seconds that gets the system time and updates the text field. This seems like a terribly…
rscott
  • 616
  • 1
  • 6
  • 9
7
votes
2 answers

Timer inside global queue is not calling in iOS

-(void)viewDidLoad{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [NSTimer scheduledTimerWithTimeInterval:0.10 target:self …
user2019279
  • 163
  • 2
  • 7
7
votes
3 answers

How to access the remaining time of a non repeating NSTimer

I'm trying to access the remaining time of an NSTimer X. I want to update the title of a button every second to reflect remaining mm:ss until zero. I couldn't find anything here. For example: [btY setTitle:[What to insert here?]…
Coltan
  • 73
  • 1
  • 3
7
votes
1 answer

What happens to my NSRunLoop and timer when the app goes into background and returns?

I have an NSRunLoop in my app connected to a timer: NSTimer *updateTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(onUpdateTimer) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:updateTimer…
dgund
  • 3,459
  • 4
  • 39
  • 64
6
votes
3 answers

NSTimer userInfo. How object is passing to the selector?

I have this code: -(void)startRotation:(RDUtilitiesBarRotation)mode { rotationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(rotateSelectedItem:) userInfo:[NSNumber numberWithInt:mode]…
NemeSys
  • 555
  • 1
  • 10
  • 28
6
votes
2 answers

Objective C & iOS: running a timer? NSTimer/Threads/NSDate/etc

I am working on my first iOS app, and have run in the first snag I have not been able to find a good answer for. The problem: I have a custom UIGestureRecognizer and have it all wired up correctly, and I can run code for each touch in the @selector…
solenoid
  • 954
  • 1
  • 9
  • 20
6
votes
3 answers

60 hz NSTimer and autoreleased memory

I have an NSTimer firing at 60 fps. It updates a C++ model and then draws via Quartz 2D. This works well except memory accumulates quickly even though I am not allocating anything. Instruments reports no leaks but many CFRunLoopTimers (I guess…
Sam
  • 2,707
  • 20
  • 25
6
votes
2 answers

Why timer stops when scrolling in UIWebView iPhone?

Possible Duplicate: UIScrollView pauses NSTimer until scrolling finishes I am having a bit of trouble figuring out why the NSTimer stops as soon as I scroll in the UIWebView. Please take a look at the following code (I know this isn't a homework…
dyang
  • 463
  • 3
  • 13
6
votes
3 answers

How to pass an argument to a method called in a NSTimer

I have a timer calling a method but this method takes one paramether: theTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(timer) userInfo:nil repeats:YES]; should be theTimer = [NSTimer…
Michele
  • 681
  • 3
  • 16
  • 27