If I use:
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(myMethod) userInfo:nil repeats:YES];
How can I invalidate this, as it's a class method I don't have access to a pointer to it?
You can make something like this:
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(myMethod:) userInfo:nil repeats:YES];
(note ":" after myMethod)
- (void) myMethod: (id) sender
{
if ([sender isKindOfClass:[NSTimer class]])
{
NSTimer* timer = (NSTimer*) sender;
[timer invalidate];
}
}
It's a class method that returns a timer. You invalidate that timer.
Related
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];
}
I have a UISlider and it works ok if i want to change value to + , but when i'm get back to - , the effect is not ok . My app is a flashlight , and i change the frequncy for flesh from slider . Any ideea which can help me please ?
The index of slider is ok . This is my code.
if ([self.lightON.currentImage isEqual:[UIImage imageNamed:#"off-1.png"]]) {
if (slider.value==0) {
AVCaptureDevice * captDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[captDevice lockForConfiguration:nil];
[captDevice setTorchMode:AVCaptureTorchModeOn];
[captDevice unlockForConfiguration];
}
if (slider.value==1){
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(toggleFlashlight) userInfo:nil repeats:YES];
}
if (slider.value==2) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(toggleFlashlight) userInfo:nil repeats:YES];
}
if (slider.value==3) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(toggleFlashlight) userInfo:nil repeats:YES];
}
if (slider.value==4) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(toggleFlashlight) userInfo:nil repeats:YES];
}
if (slider.value==5) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(toggleFlashlight) userInfo:nil repeats:YES];
}
}
I'm not 100% clear on the problem, but you should be invalidating the timer each time the slider is changed, and before you create the new timer. As it stands, each time you change the slider you will create a new timer so you will have many more calls to the toggle method than you are expecting.
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];
});
So, Here is the problem. I am trying to make a timer for my game however it doesnt seem to run.
I started by making a property for nstimer:
#property (nonatomic) NSTimer gameTimer;
and synthesizing it:
#synthesize gameTimer = _gameTimer;
then I use this method to set it:
-(NSTimer *) gameTimer{
if (_gameTimer == nil) _gameTimer = [[NSTimer alloc]init];
return _gameTimer;
}
however when i try to start the timer through a uibutton:
- (IBAction)play:(UIButton *)sender {
_levelNumber = 1;
_en1v = -1;
_en2v = 1;
_en3v = -1;
[self setPath];
_gameTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
self.play.hidden = TRUE;
}
it doesn't work.
I put an nslog into the onTimer: method and found out that the timer just isn't firing some how?
Am I making an obvious mistake?
You have not started the timer at all.
Use:
_gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
And while stopping, (may be in stop: method)
[_gameTimer invalidate];
_gameTimer = nil;
I would like to make a sequence of activities (A and B) using 2 timers.
I get some problems with timer invalidate...
Is this the correct way to proceed?
Thank you!!!
timerAStart = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(startActivityA) userInfo:nil repeats:NO];
timerAStop = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(StopA) userInfo:nil repeats:NO];
timerBStart = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(startActivityB) userInfo:nil repeats:NO];
timerBStop = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:#selector(StopB) userInfo:nil repeats:NO];
- (void) StopA {
[timerAStart invalidate];
timerAStart=nil;
}
- (void) StopB {
[timerBStart invalidate];
timerBStart=nil;
}
You don't need to invalidate since your timer is non-repeating. If you look at the documentation you'll see that non-repeating timers self-invalidate.