Use of the Nstimer class
Create a Timer
ScheduledTimerWithTimeInterval:invocation:repeats:
-(Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) ti invocation: (nsinvocation *) invocation repeats: (BOOL) Yesorno;
ScheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
-(Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno; Creates a new Nstimer object and a timesheet that returns a call to an instance method in the current default mode.
TimerWithTimeInterval:invocation:repeats:
-(Nstimer *) Timerwithtimeinterval: (nstimeinterval) ti invocation: (nsinvocation *) invocation repeats: (BOOL) Yesorno;
TimerWithTimeInterval:target:selector:userInfo:repeats:
-(Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno; –initwithfiredate:interval:target:selector:userinfo:repeats:
-(ID) initwithfiredate: (nsdate *) date interval: (nstimeinterval) TI target: (ID) T selector: (SEL) s userInfo: (ID) UI Repeats: (BOOL) rep; Scheduledtimerwithtimeinterval: (nstimeinterval) seconds
Book a timer and set a time interval. Represents a value that enters a time interval object, in seconds, of a >0 floating-point type, and if the value is <0, the system defaults to 0.1 target: (ID) Atarget represents the object being sent, such as self selector: (SEL) Aselector The method selector, within the time interval, chooses to invoke an instance method UserInfo: (ID) userInfo This parameter can be nil, and when the timer fails, the timer is reserved and freed by the object you specify. Repeats: (BOOL) Yesorno When Yes, the timer will continue to loop until it expires or is released, and when no, the timer will fail to be sent once. Invocation: (nsinvocation *) invocation start Timer–fire stop timer–invalidate Timer settings –isvalid–firedate–setfiredate:– Timeinterval–userinfo
Nstimeinterval class: Is a floating-point number used to define seconds
Example:
The iphone provides us with a very powerful time timer Nstimer
He can complete any timing function:
We are also very simple to use, just remember three elements can be, the specific three elements are: time interval nstimeinterval floating point type, event proxy
Delegate and event handling methods @selector ();
-(Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno; To initialize a time timer
Below I have written a very simple example
Initialize a timer:
-(void) Inittimer
{
Time interval
Nstimeinterval timeinterval = 1.0;
Timer
Nstimer Showtimer = [Nstimer scheduledtimerwithtimeinterval:maxshowtime
Target:self
Selector: @selector (handlemaxshowtimer:)
Userinfo:nil
Repeats:no];
}
Triggering events
-(void) Handlemaxshowtimer: (Nstimer *) Thetimer
{
NSDateFormatter dateformator = [[NSDateFormatter alloc] init];
Dateformator.dateformat = @ "Yyyy-mm-dd HH:mm:ss";
NSString *date = [dateformater stringfromdate:[nsdate Date]];
if ([Date isequaltostring:@ "2011-11-09 23:59:59"])
{
Uialertview *alert = [[Uialertview alloc] Initwithtitle:title_name
message:@ "There's a new Day right now!" ”
Delegate:self
Ancelbuttontitle:nil
Otherbuttontitles:confirm_title, nil];
[Alert show];
[Alert release];
}
[Data release];
[Dateformator release];
}
Another example is attached: box racing
-(void) viewdidload
{
[Super Viewdidload];
CGRect Workingframe;
workingframe.origin.x = 15;
WORKINGFRAME.ORIGIN.Y = 400;
WorkingFrame.size.width = 40;
WorkingFrame.size.height = 40;
for (int i = 0; i < 6; i++)
{
UIView *myview = [[UIView alloc] initwithframe:workingframe];
[MyView settag:i];//marker block
[MyView Setbackgroundcolor:[uicolor Bluecolor];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;[self.view addSubview:myView];
}
}
iOS issue summary: 2015-1-9 Nstimer class Usage (RPM)