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. IfNO
, 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"-
@end
---------------
#import "timerViewController.h"-
@implementation timerViewController
- (void)dealloc {
[super 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];
}
[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];
}
}
}
}
{
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.
}
// 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;
}
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}