iPhone Timer - How to wait seconds? - iphone

I'm trying to wait seconds ..
for (int i = 0; i < 3; i++) {
// something A
...
// wating
[NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:#selector(endTimer:)
userInfo:nil
repeats:YES];
// something B
...
}
- (void)endTimer:(NSTimer *)timer {
NSLog(#"end timer");
[timer invalidate];
}
I wanna 'A -> waiting -> B'
but don't wait ...
A -> B -> A -> B -> A -> B -> end timer, end timer, end timer
Is there any way?
good day

try this,
-(void)callA{
//Do your thing...
}
-(void)callB{
//Do your thing...
}
-(void)callFunction{
count++;
if(count<3){
[self performSelector:#select(callA) withObject:nil];
[NSThread sleepForTimeInterval:3.0];
[self callB];
}
else{
[timer invalidate];
}
}
Now, create timer in main function from which you want to call the above function.
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(callFunction) userInfo:nil repeats:YES];

Even if your question is not phrased well,
// set myCount somewhere as an INT with value 0
// int myCount = 0;
-(void)callA
{
if (myCount < 3){
[self performSelector:#selector(callB) withObject:nil afterDelay:1.0];
}
}
-(void)callB
{
myCount +=1;
[self performSelector:#selector(callA) withObject:nil afterDelay:1.0];
}

Related

Reset NSTimer after navigating to another view

Currently I am developing a game which needs a stop watch say 1 minute,when user taps the play button it enters the game screen,I want the to run the stop watch count down of 1 minute 01:00,00:59,00:58 etc. For that reason I searched and found NSTimer would be the ideal choice to implement a stop watch.Hence I took a label,created an instance of NSTimer,assigned time interval and started decrementing the value in timer label,i.e.:
-(void)viewDidAppear:(BOOL)animated
{
self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(viewWillAppear:) userInfo:nil repeats:YES];
[super viewDidAppear:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
static int currentTime = 60;
int newTime = currentTime--;
int minutesRemaining = newTime / 60; // integer division, truncates fractional part
int secondsRemaining = newTime % 60; // modulo division
self.timerLabel.text = [NSString stringWithFormat:#"%02d:%02d", minutesRemaining, secondsRemaining];
if ([self.timerLabel.text isEqualToString:#"00:00"])
{
[stopWatchTimer invalidate];
}
[super viewWillAppear:YES];
}
The problem here is the timer starts running 01:00,00:59,00:58 and goes on,say at 53rd second,I navigated to another view and came back,it is running from 00:53,00:52 and so on,but I want it to run from 01:00 and for that I implemented invalidating NSTimer in viewDidDisappear i.e.
-(void)viewDidDisappear:(BOOL)animated
{
if ([stopWatchTimer isValid])
{
[stopWatchTimer invalidate];
self.stopWatchTimer = nil;
}
[super viewDidDisappear:YES];
}
Still the same issue exists!
Done lots of Research on the issue and found no answer useful and working.
Can some one please guide me,any help is appreciated.
Thanks every one in advance :)
You are use the selector viewWillAppear for the timer. viewWillAppear is a method on a viewController that gets called when the view appears on screen, you should not call it yourself. Instead, create your own method that decrements the timer:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime:) userInfo:nil repeats:YES];
currentTime = 60; // This could be an instance variable
self.timerLabel.text = #"01:00";
}
-(void)updateTime {
int newTime = currentTime--;
int minutesRemaining = newTime / 60; // integer division, truncates fractional part
int secondsRemaining = newTime % 60; // modulo division
self.timerLabel.text = [NSString stringWithFormat:#"%02d:%02d", minutesRemaining, secondsRemaining];
if ([self.timerLabel.text isEqualToString:#"00:00"])
{
[self.stopWatchTimer invalidate];
}
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if ([self.stopWatchTimer isValid])
{
[self.stopWatchTimer invalidate];
self.stopWatchTimer = nil;
}
}
With this technique the timer is responsible for calling every second, but you will probably have timing issues after some seconds. To have a more accurate timing, you should store the time that you started the countdown and then on every updateTime call, compare the current time with the stored started time.
Add to your interface:
#property (nonatomic) NSTimeInterval startTime;
then in the implementation:
-(void)viewDidAppear:(BOOL)animated
{
self.startTime = [NSDate timeIntervalSinceReferenceDate];
self.duration = 60; // How long the countdown should be
self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(updateTime)
userInfo:nil
repeats:YES];
self.timerLabel.text = #"01:00"; // Make sure this represents the countdown time
}
-(void)updateTime {
int newTime = self.duration - (round([NSDate timeIntervalSinceReferenceDate] - self.startTime));
int minutesRemaining = newTime / 60; // integer division, truncates fractional part
int secondsRemaining = newTime % 60; // modulo division
self.timerLabel.text = [NSString stringWithFormat:#"%02d:%02d", minutesRemaining, secondsRemaining];
if (newTime < 1)
{
[self.stopWatchTimer invalidate];
/* Do more stuff here */
}
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if ([self.stopWatchTimer isValid])
{
[self.stopWatchTimer invalidate];
self.stopWatchTimer = nil;
}
}
Some extra (unrelated) comments on the code:
when implementing viewDidDisappear: pass the animated parameter to the call to super:
[super viewDidDisappear:animated];
when implementing viewDidAppear: pass the animated parameter to the call to super, also, make sure you call super first this in the method, before doing anything else

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

Duplicate action at the same time of NSTimer?

Below is my source code,i don't know why.
Can anyone explain to me?.Thank so much
- (void)onTimer:(NSTimer *)timer {
long currentPlaybackTime = self.player.currentPlaybackTime;
int currentHours = (currentPlaybackTime / 3600);
int currentMinutes = ((currentPlaybackTime / 60) - currentHours*60);
int currentSeconds = (currentPlaybackTime % 60);
self.currentLabel.text = [NSString stringWithFormat:#"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds];
if( currentMinutes > 0 && currentSeconds == 5)
{
//duplicate action at here.
NSLog(#"Make a thread to decrypt file buffer");
[self bufferEncryption];
}
}
Call timer:
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
When i try with:
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(onTimer:) userInfo:nil repeats:NO];
Timer calls only one round.Why?
Regards,
Answering your comment....you call your timer every HALF second, so (currentPlaybackTime % 60) will evaluate to 5 twice (mod will return an INTEGER). Once for 5 and once again for 5.5. That is why it gets called twice.

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

NStimer and for loop

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