How can I wait the while loop? - objective-c

Here is my code:
float interval = 0.03;
while (interval > 0.005) {
timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
NSLog(#"%f", interval);
interval = interval - 0.005;
}
How can I make the while loop to wait an specific amount of time after every loop? I've been searching thing like sleep or performSelector:withObject:afterDelay but neither they don't work nor I don't know how to use them... Some help please?

You could use a recursive structure:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[self recursiveMethode:[NSNumber numberWithDouble:0.3]];
}
-(void)onTimer
{
NSLog(#"timer");
}
-(void)recursiveMethode:(NSNumber *)interval
{
timer = [NSTimer scheduledTimerWithTimeInterval:interval.doubleValue target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
NSLog(#"%f", interval.doubleValue);
double delay = 1;
[self performSelector:#selector(recursiveMethode:) withObject:[NSNumber numberWithDouble:interval.doubleValue - 0.05] afterDelay:delay];
}

Related

Is it possible to have a UITextView scroll through text automatically, and repeatedly in Xcode?

Is it possible to have a UITextView scroll through text automatically, and repeatedly? Like, I have a UITextView, and the text just scrolls, and scrolls, and when it has reached the end, repeats itself, over and over, without the user having to scroll?
- (void)viewDidLoad
{
h=0; // add the variable int h to your interface
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(scrollPosition)
userInfo:nil
repeats:YES];
}
- (void) scrollPosition {
h += 50; // add the variable int h to your interface
//you must add the textview delegate to file's owner
[self.textview setContentOffset:CGPointMake(0, h) animated:YES];
}
-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
// at the end of scrollview
if (scrollOffset + scrollViewHeight >= scrollContentSizeHeight)
{
//you must add the textview delegate to file's owner
[self.textview setContentOffset:CGPointMake(0, 0) animated:YES];
[myTimer invalidate];
myTimer = nil;
h=0;
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(scrollPosition)
userInfo:nil
repeats:YES];
}
}

Trying to deactive NSTimer

I am using NSTimer to run a loop for counting down, then at some point(clicking 'start') I need to run this once again. The first Timer isn't stopped though so countdown pace increases with every run of the function
- (IBAction)startbutton:(id)sender {
timeTick = arc4random_uniform(7)+3;
timeofstart = CACurrentMediaTime();
chosentime = timeTick;
NSLog(#"chosentime værdien er %f", chosentime);
[timer invalidate];
timer = nil;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(tick) userInfo:nil repeats:YES];
}
- (void) tick {
if (timeTick == 0) {
winnerlabel2.text = #"GO !!!";
}
else {
timeTick --;
NSLog(#"%f", timeTick);
}
}
Here, it is unclear what timer is referring to in these first 2 lines. This 3rd line is creating a new local variable. It looks like you must have an instance variable timer that is created somewhere else. You are invalidating that timer, but then creating a new timer as a local variable that has no permanent handle (because it dies after going out of scope at the end of this method call) and thus every timer after the first never gets invalidated.
[timer invalidate];
timer = nil;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(tick) userInfo:nil repeats:YES];
Change this 3rd line from...
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(tick) userInfo:nil repeats:YES];
to...
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(tick) userInfo:nil repeats:YES];

Using NSTimer to implement retry logic with exponential backoff

I'm trying to implement retry logic with exponential backoff using NSTimer.
My code looks like this:
-(void)start
{
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self
selector:#selector(startWithTimer:) userInfo:nil repeats:NO];
}
-(void)startWithTimer:(NSTimer *)timer
{
if (!data.ready) {
// timer.timeInterval == 0.0 ALWAYS!
NSTimeInterval newInterval = timer.timeInterval >= 0.1 ? timer.timeInterval * 2 : 0.1;
newInterval = MIN(60.0, newInterval);
NSLog(#"Data provider not ready. Will try again in %f seconds.", newInterval);
NSTimer * startTimer = [NSTimer scheduledTimerWithTimeInterval:newInterval target:self
selector:#selector(startWithTimer:) userInfo:nil repeats:NO];
// startTimer.timeInteval == 0.0 ALWAYS!
return;
}
...
}
The problem I'm having is that the timer NSTimer scheduledTimerWithTimeInterval seems to ignore the interval I'm providing and always sets it to 0.0. Any suggestions on what I'm doing wrong here?
The Apple Documentation has this to say about the timeInterval property on NSTimer.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html
If the receiver is a non-repeating timer, returns 0 (even if a time interval was set).
You will need to use some other means to keep track of what the timer interval should be. I recommend an iVar on your class.
-(void)start
{
_timeInterval = 0.0;
[NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self
selector:#selector(startWithTimer:) userInfo:nil repeats:NO];
}
-(void)startWithTimer:(NSTimer *)timer
{
if (!data.ready) {
_timeInterval = _timeInterval >= 0.1 ? _timeInterval * 2 : 0.1;
_timeInterval = MIN(60.0, _timeInterval);
NSLog(#"Data provider not ready. Will try again in %f seconds.", _timeInterval);
NSTimer * startTimer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self
selector:#selector(startWithTimer:) userInfo:nil repeats:NO];
return;
}
...
}

Display an image for 1 sec on the view

What I want to realize is:
When I touch a button, the image display on the view for 1 sec, then the image disappear.
I know that a NSTimer will help, but i dont know how to write the right code...need your help, thanks.
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showPictures:) userInfo:nil repeats:NO];
}
- (void)showPictures:(NSTimer *)timer {
bodytouchedimage.hidden = YES;
}
What you should to is call the showPictures function when you touch the button and then within the showPictures method you add a NSTimer that will call a hidePictures method 1 second later
- (void)showPictures
{
bodytouchedimage.hidden = NO;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hidePictures) userInfo:nil repeats:NO];
}
- (void)hidePictures
{
bodytouchedimage.hidden = YES;
}
Rather than using NSTimer, it would be easier to simply call your method to hide the image like this:
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
[self performSelector:#selector(hidePicture) withObject:nil afterDelay:1];
}
- (void)hidePicture {
bodytouchedimage.hidden = YES;
}
performSelector:withObject:afterDelay: is a method of the NSObject class.

Objective-C help NSTimer doesn't repeat

i need a little help i have a method; countDown which is called when iTunes sends a notification, the method countDown then runs the method timerHit which gets a double minuses one from it then sets the value to a label, the method countDown is set to repeatedly run timerHit, however it doesn't seem to be working.
Here's what i have so far, any help would be much appreciated.
- (void)countDown {
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerHit:) userInfo:nil repeats:YES];
}
- (void)timerHit:(NSTimer *)p_timer {
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
if ([iTunes isRunning]) {
double trackDuration = [[iTunes currentTrack] duration];
trackDuration--;
[duration setDoubleValue:trackDuration];
}
}
Thanks, Sami.
If the timer is on a thread then you should run it on a active run loop like so:
NSRunLoop *mLoop = [NSRunLoop currentRunLoop];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerHit:) userInfo:nil repeats:YES];
mRunLoop = YES;
while (mRunLoop && [mLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]);