Friday, April 26, 2013

NSTimer scheduledTimerWithTimeInterval example ios


scheduledTimerWithTimeInterval :invocation:repeats:

Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation*)invocation repeats:(BOOL)repeats
Parameters
seconds
The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.
invocation
The invocation to use when the timer fires. The timer instructs the invocation object to retain its arguments.[NSTimer scheduledTimerWithTimeInterval]
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
Return Value of [NSTimer scheduledTimerWithTimeInterval]
A new NSTimer object, configured according to the specified parameters.
Discussion
After seconds seconds have elapsed, the timer fires, invoking invocation.
Example of [NSTimer scheduledTimerWithTimeInterval]
- (void) viewDidLoad {
NSInvocation *updateDisplayInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector: @selector (myMethod)]];
[updateDisplayInvocation setSelector: @selector (myMethod)];
[updateDisplayInvocation setTarget: self];
NSTimer *audioDisplayUpdateTimer;
audioDisplayUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 invocation:updateDisplayInvocation repeats:YES];
[super viewDidLoad];
}
- (void) myMethod {
NSLog(@"Method execution");
}

scheduledTimerWithTimeInterval :target:selector:userInfo:repeats:

Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
Parameters
seconds
The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.
target
The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.[NSTimer scheduledTimerWithTimeInterval]
aSelector
The message to send to target when the timer fires. The selector must correspond to a method that returns void and takes a single argument. The timer passes itself as the argument to this method.
userInfo
The user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.[NSTimer scheduledTimerWithTimeInterval]
repeats
If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
Return Value of [NSTimer scheduledTimerWithTimeInterval]
A new NSTimer object, configured according to the specified parameters.
Discussion
After seconds seconds have elapsed, the timer fires, sending the message aSelector to target.
Example of [NSTimer scheduledTimerWithTimeInterval]
#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;
}