scheduledTimerWithTimeInterval Example


 

scheduledTimerWithTimeInterval Example

scheduledTimerWithTimeInterval is a one of the class method of the NSTimer class, which is used to create timers. In general, we can say that NSTimer

scheduledTimerWithTimeInterval is a one of the class method of the NSTimer class, which is used to create timers. In general, we can say that NSTimer

scheduledTimerWithTimeInterval Example

scheduledTimerWithTimeInterval is a one of the class method of the NSTimer class, which is used to create timers. In general, we can say that NSTimer creates a object that sends a message to another object telling it to when to fire a timer or update it in a certain time intervals. And "scheduledTimerWithTimeInterval" schedules the timer into RunLoops that has two different methods to write it...
scheduledTimerWithTimeInterval:invocation:repeats: and
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
that basically creates the timer and schedules it on the current run loop in default mode. We will see this through a example...

In the example, first of all we'll create a timer, will pass the NSTimer object to our method and then we can stop the timer.
Creating a timer with the scheduledTimerWithTimeInterval

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(moveRect) userInfo:nil repeats:YES];

"scheduledTimerWithTimeInterval:.1" setting the duration of time for the timer, and if you set "repeats:YES" timer will repeat calling the selector every time in the given duration.

"moveRect" is the name of method that is called by @selector.. and to stop the timer we can simply write
[timer invalidate];

Example Code:
#import <UIKit/UIKit.h>

@interface timerViewController : UIViewController {

NSTimer* timer;

}
@end
---------------
#import "timerViewController.h"-

@implementation timerViewController

- (void)dealloc {
[super dealloc];
}

- (void)viewDidLoad {
[super viewDidLoad];

CGRect RectFrame;
RectFrame.origin.x = 25;
RectFrame.origin.y = 300;
RectFrame.size.width = 20;
RectFrame.size.height = 20;

for(int i = 0; i < 10; i++)
{
UIView *myView = [[UIView alloc] initWithFrame:RectFrame];
[myView setTag:i];
[myView setBackgroundColor:[UIColor orangeColor]];

RectFrame.origin.x = RectFrame.origin.x + RectFrame.size.width + 10;
[self.view addSubview:myView];
}

timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(moveRect) userInfo:nil repeats:YES];
}

-(void)moveRect
{
int r = rand() % 10;

for(UIView *aView in [self.view subviews])
{
if([aView tag] == r)
{
int movement = rand() % 100;
CGRect RectFrame = aView.frame;
RectFrame.origin.y = RectFrame.origin.y - movement;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.2];
[aView setFrame:RectFrame];
[UIView commitAnimations];

if(RectFrame.origin.y < 0)
{
[timer invalidate];
}
}
}
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

@end

On running the application... it should look like the given image

Download Code

Ads