Uislider value when get back to minus - ios

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.

Related

Display a message using NSTimer

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...
}

Slow down NSTimer with a for loop in Objective-C

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];
}

How do I have one timer countdown and the other randomly move an object?

I have the right code to move an object randomly and also to make numbers count down but I am not able to have an object move while the timer counts down. One thing I found weird though was that when I took out the part about the TimeLeft label the objects moved randomly but obviously the numbers did not count down.
The main question is: how can I have a timer countdown and have the object move at the same time?
(my main goal is to make the object stop moving once the timer reaches zero)
I would appreciate it alot if someone could help with this problem?
-(void)Workauto{
secondsCount1 = 10;
autoperiods = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(autoperiod) userInfo:nil repeats:YES];
}
-(void)autoperiod{
secondsCount1 = secondsCount1 -1;
int minuts = secondsCount1/ 60;
int seconds = secondsCount1 - (minuts * 60);
NSString *timerOutput = [NSString stringWithFormat:#"%2d:%.2d", minuts , seconds];
Count.text = timerOutput;
if (secondsCount1 == 0){
secondsCount1=0;
[autoperiods invalidate];
autoperiods=nil;
Count.hidden=YES;
AutoTimeLeftLabel.hidden=YES;
TimeLeft.hidden=NO;
TimeLeftlabel.hidden=NO;
Ball.hidden=NO;
Ball2.hidden=NO;
BluBall.hidden=NO;
BluBall2.hidden=NO;
RedHigh1.hidden=YES;
RedHigh2.hidden=YES;
RedHigh3.hidden=YES;
RedHigh4.hidden=YES;
RedLow1.hidden=YES;
RedLow2.hidden=YES;
BlueHigh1.hidden=YES;
BlueHigh2.hidden=YES;
BlueHigh3.hidden=YES;
BlueHigh4.hidden=YES;
BlueLow1.hidden=YES;
BlueLow2.hidden=YES;
RedMid1.hidden=YES;
RedMid2.hidden=YES;
RedMid3.hidden=YES;
RedMid4.hidden=YES;
BlueMid1.hidden=YES;
BlueMid2.hidden=YES;
BlueMid3.hidden=YES;
BlueMid4.hidden=YES;
move = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:#selector(move) userInfo:nil repeats:YES];
pos = CGPointMake(4.0, -4.0);
move2 = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:#selector(move2) userInfo:nil repeats:YES];
pos2 = CGPointMake(3.0, 4.0);
Update = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
DPad = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(DPad) userInfo:nil repeats:YES];
//RedGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(RedGoals) userInfo:nil repeats:YES];
//BlueGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(BlueGoals) userInfo:nil repeats:YES];
[self SetTimer];
}
}
-(void)SetTimer{
comeonandcount = 150;
GameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(GameTimerCo) userInfo:nil repeats:YES];
}
-(void)GameTimerCo{
comeonandcount = comeonandcount - 1;
int minuts = comeonandcount / 60;
int seconds = comeonandcount - (minuts * 60);
NSString *timerOutputGame = [NSString stringWithFormat:#"%2d:%.2d", minuts , seconds];
TimeLeft.text = timerOutputGame;
if (comeonandcount == 0){
comeonandcount=0;
[GameTimer invalidate];
GameTimer=nil;
[move invalidate];
[move2 invalidate];
}
}
One thing I found weird though was that when I took out the part about the TimeLeft label the objects moved randomly but obviously the numbers did not count down.
Very well done! You've actually solved the problem already. That part is not "weird"; it's the cause of the problem.
You are using autolayout. Well, when you set the text of a label, that causes layout to happen. Thus, the constraints throughout your interface assert themselves and put all your objects back where they were. It's as simple as that.

NSTimer not firing, has correct method signature

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];
});

NSTimer - sequence of activities - how to invalidate timer?

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.

Resources