Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS - Creating & Using Interval Specific Timers

I am a newbie IOS developer, but I have a good amount of experience in Android development. My question is regarding the creating and use of interval specific timers.

In android I could easily make a timer like this:

timedTimer = new Timer();
    timedTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {

            TimedMethod();
        }

    }, 0, 1000);

Where the interval is 1000 MS and the method TimedMethod() is called on every tick. How would I go about implementing a similar function in IOS?

Thanks so much for reading! Any help at all would be great! :-)

like image 810
user879702 Avatar asked Aug 10 '11 00:08

user879702


People also ask

Can I develop my own iOS app?

Thousands of iOS apps are built and published each day through Appy Pie's iOS app builder platform! Create your own native iOS mobile apps within a few minutes. Appy Pie's iPhone app builder gives you the perfect tools to create an app for the iOS with zero coding.

Can I create an iOS app for free?

You can learn how to develop apps for Apple platforms for free without enrolling. With just an Apple ID, you can access Xcode, software downloads, documentation, sample code, forums, and Feedback Assistant, as well as test your apps on devices.


2 Answers

You can use a repeating NSTimer like so:

- (void) startTimer {
   [NSTimer scheduledTimerWithTimeInterval:1 
                                    target:self 
                                  selector:@selector(tick:) 
                                  userInfo:nil
                                   repeats:YES];
}

- (void) tick:(NSTimer *) timer {
   //do something here..

}
like image 87
Jacob Relkin Avatar answered Sep 20 '22 03:09

Jacob Relkin


Use

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];

In the same class as you called the above method, create a method called timerCallback. This will be called every time your timer fires; every 1000 milliseconds.

like image 27
Benjamin Mayo Avatar answered Sep 23 '22 03:09

Benjamin Mayo