The timeInterval of NSTimer is readOnly,we can not change it directly.Is there any way to do it?
Becasue the timeInterval of NSTimer is readOnly so we can not change it directly.
The way to change timeInterval is redefine the timer,such as:
- (void)changeTimeIntervalWithInterval:(NSInteger)timeInterval{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:#selector(printHH) userInfo:nil repeats:YES];
}
}
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.
This question already has answers here:
How can I programmatically pause an NSTimer?
(16 answers)
Closed 9 years ago.
I have a game which uses a timer. I want to make it so the user can select a button and it pauses that timer and when they click that button again, it will unpause that timer. I already have code to the timer, just need some help with the pausing the timer and the dual-action button.
Code to timer:
-(void)timerDelay {
mainInt = 36;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(countDownDuration)
userInfo:nil
repeats:YES];
}
-(void)countDownDuration {
MainInt -= 1;
seconds.text = [NSString stringWithFormat:#"%i", MainInt];
if (MainInt <= 0) {
[timer invalidate];
[self delay];
}
}
That's very easy.
// Declare the following variables
BOOL ispaused;
NSTimer *timer;
int MainInt;
-(void)countUp {
if (ispaused == NO) {
MainInt +=1;
secondField.stringValue = [NSString stringWithFormat:#"%i",MainInt];
}
}
- (IBAction)start1Clicked:(id)sender {
MainInt=0;
timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countUp) userInfo:Nil repeats:YES];
}
- (IBAction)pause1Clicked:(id)sender {
ispaused = YES;
}
- (IBAction)resume1Clicked:(id)sender {
ispaused = NO;
}
There is no pause and resume functionality in NSTimer. You can impliment it like below code.
- (void)startTimer
{
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)fireTimer:(NSTimer *)inTimer
{
// Timer is fired.
}
- (void)resumeTimer
{
if(m_pTimerObject)
{
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)pauseTimer
{
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
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.
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];
}
}