I have a NSTimer and a NSSlider in my app. How do I make, that the time interval of the timer would respond instantly to the slider value?
For now it responds just at the beginning. Once the timer is already fired, it doesn't respond any more...
[NSTimer scheduledTimerWithTimeInterval:[slider doubleValue]
target:self
selector:#selector(updateTextFieldWithRandomNumber)
userInfo:nil
repeats:YES];
You cannot change the time interval of a timer once you have created it. You have to invalidate the old timer and create a new one with the new time interval.
What about putting a KVO observation on the slider's doubleValue property and invalidating the timer and recreating it when the notification happens?
Related
I am using xcode and am having a problem moving a button automatically. I have this function that whenever I call it, I expect the button to move to the coordinates that I set:
[movebutton setCenter:CGPointMake(164,50)];
Previously I tried to set an NStimer in an IBAction function and then use the timer to call this movebutton function - the button moved but if I call the same function without an NStimer it no longer works.
The code for the timer is:
[NSTimer scheduledTimerWithTimeInterval:(0.1/3) target:self selector:#selector(movebutton:) userInfo:nil repeats:YES];
Can anyone spot what I am missing?
Update:
I try to print out the x and y coordinates of the button and actually the button position has been updated.
NSLog(#"x position %f",movebutton.frame.origin.x);
however, on the UI screen it does not reflect at all.
Try placing
[self.view layoutIfNeeded];
in your moveButton: method so that when it's called from the timer it knows to update the UI.
Where do you call -[UIButton setCenter] manually? Couldn't comment because I don't have permission to comment.
The NSTimer works because it fires the method every 0.1/3 because the NSTimer instance is added to run loop
Is it possible to make a NStimer that fires first when it is initialized and then afterwards on a 0.01 seconds schedule? I have this code..
self.displayTimerTotalTime = [NSTimer timerWithTimeInterval:0.01
target:self
selector:#selector(timerFiredTotalTime:)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.displayTimerTotalTime forMode:NSRunLoopCommonModes];
Problem is that the user can stop the timer multiple times and run it again, but this creates a 0.01 delay every time that can cause problems for the user experience. It is not good enough to check for the delay later and remove it.
You can call the -fire method to fire the timer at any time. Just add this after the above code:
[self.displayTimerTotalTime fire];
You could always call the selector yourself:
[self timerFiredTotalTime:self.displayTimerTotalTime];
If I start an NSTimer like this:
#property (strong) NSTimer * messageTimer;
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:YES];
Does it continue to run when I switch to different view controllers?
Until I cancel it with:
[messageTimer invalidate]; self. messageTimer = nil;
Yes.
Okay, now here is an extended description. NSTimer registers itself on nearest NSRunLoop, that is, current dispatch loop (they may nest). This loop asks various sources for events and calls corresponding callbacks.
When it is time for NSTimer to fire, it returns YES to NSRunLoop and that runs passed callback. There is no such thing as "other current view controller". It is all about first responder and view hierarchy, neither doesn't have any effect on run loops.
I have set up an NSTimer which after one second should perform a instance method called animate
My code looks like this:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(animate) userInfo:nil repeats:NO];
[timer fire];
This code is put into a touchesBegan method. The method DOES GET CALLED but not after one second it just immediately gets called. Why is this?
You've scheduled a timer and normally that should automatically invoke after 1.0 seconds, but you follow up with a [timer fire] call and that is immediately firing the timer and sending a message to the selector.
Look at the documentation.
Delete [timer fire];
That will solve your problem.
When you call [timer fire] it immediately fires the message to the receiver. You just need to remove that line.
Calling fire causes the message to be sent to its target immediately. See the documentation here.
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 labels)..... how can I get around this problem?
EDIT: here's the code that starts the timer:
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerDidUpdate:) userInfo:nil repeats:YES];
You should add timer into MainRunLoop as given below:
NSRunLoop * rl = [NSRunLoop mainRunLoop];
[rl addTimer:timer forMode:NSRunLoopCommonModes];
I'm guessing the timer resumes as soon as you stop interacting with the NSStatusItem? (After the menu's dismissed & mouse button released).
The user interaction puts the main run loop into a mode where it doesn't update timers, so if your label has to continually update, you'll probably need to move the NSTimer and the label drawing to a separate process or another thread.