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.
Related
NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
How can I use for display the message "your time is over" after 5 min?
In below code, Instead 5.0 you can put your time in second
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(targetMethod) userInfo:nil repeats:NO];
-(void)targetMethod {
//enter code here to execute...
}
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];
});
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];
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.