NStimer and for loop - iphone

I'm struggling with this. I saw some code where you can do this :
- (void)startTimer {
pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doActions) userInfo:nil repeats:YES];
}
Then call it in doActions.
Problem is i want to call it while a button is being pressed, and do actions is an IBaction. I keep getting a sigAbrt.
Can someone give me some sample code where you cay change a label from 'on' to 'off' every 1 second while a button is being pressed?
EDIT
i mean if doActions looks like this
- (IBAction) doActions {
for(int j; j<100; j++){
theLabel.hidden != theLabel.hidden;
//Does invalidate timer go here?
}
}

It still isn't clear to me, what you're trying to accomplish: I would have found it much easier, if you simply had it written down in plain english, after all.
That said, here's what I think will get you where you want to go:
// Assuming ivars:
// NSTimer* toggleTimer
// NSUInteger toggleCount
- (void)toggleTimerFired:(NSTimer*)timer
{
if ( toggleCount++ >= 100 ) {
[self stopToggling:self];
return;
}
// do whatever you need to do a hundred times here
}
- (IBAction)stopToggling:(id)sender
{
[toggleTimer invalidate], toggleTimer = nil; // you don't want dangling pointers...
// perform any other needed house-keeping here
}
- (IBAction)startToggling:(id)sender
{
[self stopToggling:self]; // if `startToggling:` will NEVER be called when a timer exists, this line CAN be omitted.
toggleCount = 0;
toggleTimer = [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:#selector(toggleTimerFired:) userInfo:nil repeats:YES];
}
Depending on what exactly you want to do, startToggling: needs to be sent on either touchUpInside or touchDown. In the second case, stopToggling: probably needs to be called on any touchUp... event of the button.

- (void)startTimer {
pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doActions) userInfo:nil repeats:YES];
}
- (void) doActions {
theLabel.hidden != theLabel.hidden;
}

Related

cancelPreviousPerformRequestsWithTarget

cancelPreviousPerformRequestsWithTarget is not working for me, i checked every one saying its working fine but for me not
i call the following in performselector
[self performSelector:#selector(Opacity:) withObject:self.objopcarray afterDelay:reversedelay];
now i want to cancel it on a button click before it fires so i did the following
- (void) pressNextButton:(id)sender
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(Opacity:) object:self.objopcarray];
if (self.animationON == YES)
{
if (self.panel < 16)
{
[self DisplayImages];
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:sketchanimduration target:self selector:#selector(DisplayImages) userInfo:nil repeats:YES];
if(self.panel > 16)
{
self.panel = 0;
}
}
}
}
but still same result
any help will be highly appreciated

NSTimer not running

I've got a program in which I want to code a timer which checks wether the user is idle or not. For that I wrote following code:
if (!idleTimer) {
NSLog(#"make");
idleTimer=[[NSTimer alloc]initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:10.0]interval:10 target:self selector:#selector(idleTimerExceeded) userInfo:nil repeats:NO];
NSLog(#"madetimer with: %f, %#", idleTimer.timeInterval, idleTimer.fireDate);
}else {
NSLog(#"no reset timer: %f", idleTimer.timeInterval);
if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < 9) {
NSLog(#"reset");
[idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
}
But somehow my logs show that the interval is always 0.0000 . Which means that something is wrong here. Can anybody help me?
ive never seen a timer look so complicated. try this:
write a method that checks if the user is idle (lets say idleChecker)
then make the timer repeatable and calls that method idleChecker
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
remember to declare the idleChecker method in the .h file
take note if u want to stop the timer event then you need to maintain a reference to it
NSTimer aTimer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
then to stop it
[aTimer invalidate];
as for checking if its running i would just stick a nslog message in there stating something like "check for idle user"

progress bar not showing the step by step progress in iphone

Hi all im using progress bar for indicating the status of xml parsing in my app.but it is not showing the step by step updation,instead the progress bar is loading after the entire xml parsing is over,till that time it is stuck and not showing any progress.how to solve this issue.can anyone help me please.
I use the below code
IBOutlet UIProgressView * threadProgressView;
threadProgressView.progress = 0.0;
[self performSelectorOnMainThread:#selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
- (void)makeMyProgressBarMoving
{
float actual = [threadProgressView progress];
if (actual < 1) {
threadProgressView.progress = actual + ((float)1/(float)10);
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
}
else{
/* something else */
}
}
Create a timer on the main thread for every 1/10th of a second. Have it hit your "makeMyProgressBarMoving" function.
myTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(makeMyProgressBarMoving) userInfo:nil repeats:YES];
Then edit your function to look like this:
- (void)makeMyProgressBarMoving {
float actual = [threadProgressView progress];
if (actual < 1) {
threadProgressView.progress = actual + ((float)1/(float)10);
return;
}
//if you got here that means the progress view is greater than 1 so now you can kill your timer that you had running every 1/10th of a second.
[myTimer invalidate];
}

iOS Camera Countdown Timer

I'm looking for the cleanest way to have a countdown timer fire when a user hits the 'take picture' button. Is there any easy way to do this?
One solution I'm thinking of is to just have a label that updates every second, but is there a way to get it working like Photobooth?
Also, at the last second before the photo is about to be taken I'd like for an image to be displayed briefly while the image is being taken. How can I go about this?
Any help would be great, thanks!
- (IBAction)takePicture:(id)sender {
theTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(updateLabel:) userInfo:nil repeats:NO];
}
- (void)updateLabel:(NSTimer *)timer {
_timeLabel.text = [NSString stringWithFormat:#"%d", time];
time = time - 1;
if (time == 0) {
[theTimer invalidate];
[_timeLabel performSelector:#selector(setText:) withObject:#"Photo taken!" afterDelay:1.0];
//Code for image shown at last second
} else {
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateLabel:) userInfo:nil repeats:NO];
}
}
Hope this helps ;)

run timer automatically for first and second time

I am new to iPhone,
What I want is that my timer call a function every second but for the very first time it must call the function at 2 second.
Any help will be really appreciated. Thanks
take a variable
BOOL isNotFirstTime;
And in the function that gets triggered after each 4 secs add the following code at the starting
-(void)AfterOneSecondsFuction
{
if(!isNotFirstTime)
{
isNotFirstTime=YES;
return;
}
//YOUR CODE HERE...
}
This is how you give a call to the function
NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(AfterOneSecondsFuction) userInfo:nil repeats:YES];
First schedule a timer and have a BOOL variable say isFirstTime set as YES;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doSomething) userInfo:nil repeats:YES];
and in
- (void)doSomething
{
if(isFirstTime)
{
isFirstTime = NO;
return;
}
// Do something
}