iOS blink flashlight with UISwitch, UISlider and NSTimer - ios

I'm making a flashlight app and i'm in problem
how to blink the flashlight with uiswitch, uislder and nstimer.
i found something on web but I can't understand it.
I already done how to turn on the flashlight(LED)
but I can't find how to blink it.
Also, I can't use NSTimer well.
self.blinkTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(timerUpdate) userInfo:nil repeats:YES];
I found this code in Stack Overflow but #selector(timerUpdate) and scheduledTimerWithTimeInterval:0.3 is problem.
my plans are
uislider's value --> scheduledTimerWithTimeInterval:(uislder's value)
uiswitch --> start blinking the flashlight.
- (void)toggleFlashlight
{
}
- (IBAction)blink:(UISwitch *)sender {
}
- (IBAction)blinkspeed:(UISlider *)sender {
int progress = lroundf(sender.value);
self.blinksliderlabel.text = [NSString stringWithFormat:#"%dms", progress];
}
Sadly, I don't know.. how to insert the code there...
Please help :(

Related

Scoring stops and resets game

I'm making a game where you are supposed to move an image between other image without colliding. Everything worked fine until I added coins to the game. When the image hits a coin, it's supposed to make a sound (which it does) and add +1 to the coin label. The problem is, when I add the code to make the coinlabel get +1, the image that I'm moving will stop and go back to starting position. The same thing happened to me when I was using an NSTimer (because users playing the app had time on finish the level.) But everytime the label counted a number, the game stopped again and the ball was put back to the starting position.
Here is some code for the game.
-(IBAction)left {
goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
if (goLeft == nil) {
goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
}
}
-(IBAction)stopLeft {
[goLeft invalidate]; goLeft = nil;
}
-(void)goLeft {
image.center = CGPointMake(image.center.x -3.5, image.center.y);
{
//Coins
if (CGRectIntersectsRect(image.frame, coin1.frame)) {
coin1.hidden = YES;
[self getcoins];
}
-(void)getcoins{
//Here i have a avaudioplayer playing a noise (dont need to show that as that works fine)
//Then the +1 to label
coinlabel.text = [[NSNumber numberWithInt:([coinlabel.text intValue] + 1)] stringValue];
// And when i add this and the image hits the coin, the image goes back to the starting position. But i want it not to stop or anything, but continue.
}
The left action is connected to touch down. And stopleft is connected to touch up inside. Hope you understand and thanks for any help.

UILabel updating incorrectly with NSTimer

I am showing a count down timer using UILabel and NSTimer -
-(void)a_Method
{
[coolTimeLbl setNeedsDisplay];
coolTime = 5; // it is an int
coolingTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(cooling) userInfo:nil repeats:YES]; // NSTimer
}
-(void)cooling
{
if (coolTime>0)
{
coolTime = coolTime-1;
NSLog(#" coolTime----%#",coolTime);
coolTimeLbl.text =[NSString stringWithFormat:#"%d",coolTime];
NSLog(#" coolTimeLbl----%#",coolTimeLbl.text);
}
else
{
[coolingTimer invalidate];
coolingTimer = nil;
}
}
The first time everything works fine and I am getting coolTimeLbl.text as - 4 3 2 1 0
But the second time when I call aMethod, coolTimeLbl is not getting updated properly - it is like 3 2 0 etc (some weird behavior)
However both NSLogs (coolTime & coolTimeLbl) print perfectly all the times and values.
Why does this happen? I tried many ways like NSNotification etc.
Please help me to fix this.
If you're calling a_Method more than once before coolingTimer invalidates itself, the timer will tick more than once.
You should add some boolean like ;
BOOL isTimerValid;
in a_Method,
if(!isTimerValid)
{
isTimerValid = YES;
coolingTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(cooling) userInfo:nil repeats:YES]; // NSTimer
}
in cooling,
else
{
isTimerValid = NO;
....
}
Had the same issue in one of my viewControllers and another one was working OK with same NSTimer code. Looked at about 20 SO threads to get it solved. No luck. In my case
myLabel.opaque = false
solved it.
Don't ask me why.

How do I prevent a UITapGestureRecognizer from recognizing single taps in rapid succession?

I have a view with a UITapGestureRecognizer instance assigned to it. It correctly responds when the user taps once, but I'd like to prevent it from recognizing again if the user taps again within a short period of time.
I'm using this in a game where the user taps on locations to find hidden objects. I'm trying to prevent the "tap like crazy all over the screen" strategy from working.
Is there a simple solution to this?
I would not recommend using an NSTimer for resolutions less than 1 second. Plus, it has more overhead. Read this answer for more information on NSTimer vs CACurrentMediaTime().
- (IBAction)handleTap:(UITapGestureRecognizer *)tgr {
static NSTimeInterval previousTapTime = 0.0; // Or an ivar
if ((CACurrentMediaTime() - previousTapTime) > 1.0) {
// A valid tap was detected, handle it
}
previousTapTime = CACurrentMediaTime();
}
Use a timer to determine whether to accept the tap or not.
Create a BOOL ivar named something like denyTap. Also add an NSTimer ivar named tapTimer.
Then in your tap recognizer method you do something like:
- (void)tapHandler:(UITapGestureRecognizer *)gesture {
if (!denyTap) {
dentTap = YES;
// process the tap as needed
// Now setup timer - choose a desired interval
tapTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(tapTimer:) userInfo:nil repeats:NO];
}
}
- (void)tapTimer:(NSTimer *)timer {
denyTap = NO;
tapTimer = nil;
}

Undeclared selector warnining in ios 7

I'm tying create a welcome label using NSTimer but its showing the through some warnings
like
undeclared selector hidelable and unused variable timer
I have not used the NSTimer before can one pls tell me where im doing wrong and wt is the right method to do it.I need to give a welcome message when app load after few minities it has to disappear
i have tried this one im not able to get pls help me
this is the code i have to used in the view didload
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(hideLabel:) userInfo:nil repeats:NO];
in storyboard i have used ib label which i want to display message
#property (strong, nonatomic) IBOutlet UILabel *wel;
please any one tell wt is proper way to make this one..
you have not declared hideLabel method.hence it gives that warning
[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(hideLabel:) userInfo:nil repeats:NO];
-(void)hideLabel:(NSTimer *)timer{
myLabel.hidden=YES;
}
Try this
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(timerCalled) userInfo:nil repeats:YES];
-(void)timerCalled
{
NSLog(#"Timer Called...");
}
According to your code, You didn't use timer anywhere else, and didn't fire repeatedly. Then you can use this code as below..
[self performSelector:#selector(hideLabel:) withObject:yourLabel afterDelay:60];
Importantly, define your target method,
-(void)hideLabel:(UILabel*)label
{
// your code here...
label.text = #"Fired...";
}
If you does not need to use (extra use of) object of NSTimer then you should create NSTimer such like,
[NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:#selector(hideLabel:) userInfo:nil repeats:NO];
And then you need to declare timer's method other wise after active (60 sec.) timer you will be get error.
- (void)hideLabel:(NSTimer *)theTimer
{
// Timer method code;
}

Initialize UISegmentedControl storyboard index

EDIT
Maybe I have described the problem incorrectly. Let me add that this segmented controller is controlling the speed of a NSTimer with the following code.
- (IBAction) segmentAction:(id)sender
{
speed = [[speeds objectAtIndex:[sender selectedSegmentIndex]] integerValue];
}
- (IBAction)startPause: (UIButton *)sender{
NSString *buttonTitle = sender.currentTitle;
if ([buttonTitle isEqualToString: #"Pause"]) {
[self.myButton setTitle:#"Resume" forState:UIControlStateNormal];
[timer invalidate];
}
else
{
[self.myButton setTitle:#"Pause" forState:UIControlStateNormal];
if ([self.deal length]>cardNum) {
timer = [NSTimer scheduledTimerWithTimeInterval:speed
target:self
selector:#selector(dealCard)
userInfo:nil
repeats:YES];
}
else
{
[timer invalidate];
}
}
}
When the user taps any of the segments in the segmentedController BEFORE tapping the startPause button, the NSTimer works fine. Nut if the user taps the startPause button before tapping a segment, no (desired) delays occur from the NSTimer. Does this help explain the situation any better?
EDIT
I have a UISegmentedControl in Storyboard and want to initialize it. It would be especially nice to initialize it in the Storyboard itself. The image below shows how the Med segment is selected, but when the app starts, Med is NOT selected; rather the user has to trigger a Value changed event. How do I get it selected, initially? One of the other questions here on SO suggested the following code, but I have not figured out how to associate the UISegmentedControl in my Storyboard with the name segmentControl.
segmentControl.selectedSegmentIndex=-1;

Resources