Greeting !!
The following codes are now I am doing :
BOOL bJobDone = NO ;
bBrickDropDone = NO ; //global var here !!
NSTimer* timer = nil ;
while(bJobDone==NO)
{
if(timer == nil)
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.3
target:self
selector:#selector(dropBrick:)
userInfo:nil
repeats:YES];
}
if(bBrickDropDone==YES)
{
bBrickDropDone = NO ;
[timer invalidate] ;
timer = nil ;
if([self MarkBrickBomb]==YES)
{
bJobDone = NO ;
[self dealBomb] ;
if([self AllClean]==YES)
{
bJobDone = YES ;
igameidx = igameidx + 1 ;
}
}else
{
bJobDone = YES ;
}
}//if(bBrickDropDone==YES)
}//while bJobDone==NO
As you can see , the timer call dropBrick function once for 0.3 second ,
if bBrickDropDone is finally = YES (dropBrick function modify this to YES if something happen)
it will process another function MarkBrickBomb and goes on until bJobDone=YES,break out of loop!!
I think my code is lousy , I should not check bBrickDropDone flag in the while loop,
because it is not efficient , also cost resource a lot !!
I need a timer to get an animated UIImageView switch , buy Still don't like
to check the done flag in while loop(it is not good ,I think) , so what can I do
in this case ? Can I get a hint for this ?
And , sorry for my english !!
The timer can never fire whilst bJobDone == NO, this is because, NSTimers are added to the NSRunLoop and can only fire whilst waiting for an event.
Related
I have an UIButton that the user must click on it 5 times in three seconds, im trying to implement a method for that, but im getting the correct result if the user clicked on the button 5 times in 3 seconds in a row, if the user clicked once and stopped for 2 seconds for example, the counter take the first click in the calculation.
in a short words, i need a method that detect the last five clicks and know if the clicks were in three second or not...
Here is my old code:
-(void)btnClicked{
counter++;
if (totalTime <=3 && counter==5) {
NSLog(#"My action");
// My action
}}
I know that my code is too simple, so that why i asked you pro's
Try altering this example appropriately:
// somewhere in the initialization - counter is an int, timedOut is a BOOL
counter = 0;
timedOut = NO;
- (void)buttonClicked:(UIButton *)btn
{
if ((++counter >= 5) && !timedOut) {
NSLog(#"User clicked button 5 times within 3 secs");
// for nitpickers
timedOut = NO;
counter = 0;
}
}
// ...
[NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:#selector(timedOut)
userInfo:nil
repeats:NO
];
- (void)timedOut
{
timedOut = YES;
}
Simply have an array with the timestamp of the last four clicks, and every time there is a click, check if the previous four are within 3 seconds from the current time. If it is not the case, discard the oldest timestamp and replace with current time, but if it is the case, you got your event and you can clear the array so that they are not used in the next 5-clicks-in-3-seconds event.
Here is 'my version' of H2CO3's code. This should suit your requirement better.
int counter = 0;
BOOL didTimeOut = NO;
- (void)buttonClicked:(UIButton *)button {
counter ++;
if (counter == 1) {
didTimeOut = NO;
[NSTimer scheduledTimerWithTimeInterval:3.0f
target:self
selector:#selector(timedOut)
userInfo:nil
repeats:NO
];
} else {
if ((counter >= 5) && !didTimeOut) {
//Do your action as user clicked 5 times in 3 seconds
counter = 0;
didTimeOut = NO;
}
}
}
- (void)timedOut {
didTimeOut = YES;
}
I have a -(void) method being executed and at one point it gets to a while loop that gets values from the accelerometer directly the moment it asks for them
I went throughout the documentation regarding the NSTimer class but I couldn't make sense of how exactly I am to use this object in my case :
e.g.
-(void) play
{
......
...
if(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9 )
{
startTimer;
}
while(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9)
{
if(checkTimer >= 300msec)
{
printOut_AccelerationStayedBetweenThoseTwoValuesForAtLeast300msecs;
break_Out_Of_This_Loop;
}
}
stopTimerAndReSetTimerToZero;
.....
more code...
....
...
}
Any help?
You cannot do it with NSTimer, because it needs your code to exit in order to fire. NSTimer uses the event loop to decide when to call you back; if your program holds the control in its while loop, there is no way for the timer to fire, because the code that checks if it's time to fire or not is never reached.
On top of that, staying in a busy loop for nearly a second and a half is going to deplete your battery. If you simply need to wait for 1.4s, you are better off calling sleepForTimeInterval:, like this:
[NSThread sleepForTimeInterval:1.4];
You can also use clock() from <time.h> to measure short time intervals, like this:
clock_t start = clock();
clock_t end = start + (3*CLOCKS_PER_SEC)/10; // 300 ms == 3/10 s
while(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9)
{
if(clock() >= end)
{
printOut_AccelerationStayedBetweenThoseTwoValuesForAtLeast300msecs;
break_Out_Of_This_Loop;
}
}
NSTimer works a little different than you want. What you need is a counter for your timer, to get how many times it looped. If your counter gets on 14 (if it's an integer), you can invalidate it.
//start timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(play:)
userInfo:nil
repeats:YES];
//stop timer
[timer invalidate];
You can create your function without that while.
- (void)play {
......
...
counter++; //declare it in your header
if(counter < 14){
x++; //your integer needs to be declared in your header to keep it's value
} else {
[timer invalidate];
}
useValueofX;
}
Take a look at the documentation.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Change the time interval of a Timer
So I have a timer:
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
And I would like that the timer decrease every ten seconds, that scheduledTimerWithTimeInterval goes to 1.5 after ten seconds then to 1.0 after 10 seconds... Is it possible to do this, and if it is possible, how can I do it?
You can't modify a timer after you created it. To change the timer interval, invalidate the old timer and create a new one with the new interval.
You don't have to use several timers, all you have to do is add a variable to use for the time interval, then create a method that invalidates the timer, changes the variable and starts the timer again. For instance you could make a method that starts the timer.
int iTimerInterval = 2;
-(void) mTimerStart {
timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
}
-(void) mTimerStop {
[timer invalidate];
iTimerInterval = iTimerInterval + 5;
[self mTimerStart];
}
This would be the simple way to decrease the timer interval and keep the timer going, but I would personally prefer using the one below, because it makes sure that the timer has only ran once, that way it will not duplicate the instance, forcing your app to become glitchy and it also makes things easier for you.
int iTimerInterval = 2;
int iTimerIncrementAmount = 5;
int iTimerCount;
int iTimerChange = 10; //Variable to change the timer after the amount of time
bool bTimerRunning = FALSE;
-(void) mTimerToggle:(BOOL)bTimerShouldRun {
if (bTimerShouldRun == TRUE) {
if (bTimerRunning == FALSE) {
timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:#selector(mCreateNewImage) userInfo:nil repeats:YES];
bTimerRunning = TRUE;
}
} else if (bTimerShouldRun == FALSE) {
if (bTimerRunning == TRUE) {
[timer invalidate];
bTimerRunning = FALSE;
}
}
}
-(void) mCreateNewImage {
//Your Code Goes Here
if (iTimerCount % iTimerChange == 0) { //10 Second Increments
iTimerInterval = iTimerInterval + iTimerIncrementAmount; //Increments by Variable's amount
[self mTimerToggle:FALSE]; //Stops Timer
[self mTimerToggle:TRUE]; //Starts Timer
}
iTimerCount ++;
}
-(void) mYourMethodThatStartsTimer {
[self mTimerToggle:TRUE];
}
I didn't finish all of the coding, but this is most of what you will need. Just change a few things and you'll be good to go!
You will need a set or sequence of timers, one for each different time interval. Stop/invalidate one timer and start the next timer when you want to change to the next time interval in your time sequence.
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;
}
I want to change my NSTimer code to to use applicationSignificantTimeChange
This is what I have right now.
Basically I want to change the timer, instead of changing every 5 seconds for example I want these textLabels to change at 12am every night anyone help me out?
h file
NSTimer *timer;
IBOutlet UILabel *textLabel;
m file
- (void)onTimer {
static int i = 0;
if ( i == 0 ) {
textLabel.text = #"iphone app";
}
else if ( i == 1 ) {
textLabel.text = #" Great App!";
}
else if ( i == 2 ) {
textLabel.text = #" WOW!";
}
else {
textLabel.text = #" great application again!!";
i = -1;
}
i++;
}
timer =[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
Did you check initWithFireDate:interval:target:selector:userInfo:repeats: method of NSTimer? Click here for more details