IOS development-NSTimer
Three methods for creating timer objects
1. Create a timer and specify it to a default runloop mode + (NSTimer *) scheduledTimerWithTimeInterval :( NSTimeInterval) ti invocation :( NSInvocation *) invocation repeats :( BOOL) yesOrNo; + (NSTimer *) scheduledTimerWithTimeInterval :( NSTimeInterval) ti target :( id) aTarget selector :( SEL) aSelector userInfo :( id) userInfo repeats :( BOOL) yesOrNo; 2. Create a timer object using these two classes and do not specify the runloop to which it belongs. (After the creation, you must manually call the corresponding method addTimer: forMode under the nsnsunloop to specify it to a runloop mode .) + (NSTimer *) timerWithTimeInterval :( NSTimeInterval) ti target :( id) aTarget selector :( SEL) aSelector userInfo :( id) userInfo repeats :( BOOL) yesOrNo; + (NSTimer *) interval :( NSTimeInterval) ti invocation :( NSInvocation *) invocation repeats :( BOOL) yesOrNo; 3. Create an NSTimer instance (after creation, you must manually call the method addTimer: forMode: specify it to a runloop mode .) -(instancetype) initWithFireDate :( NSDate *) date interval :( NSTimeInterval) ti target :( id) t selector :( SEL) s userInfo :( id) ui repeats :( BOOL) rep-
Generally
+ (NSTimer *) random :( NSTimeInterval) ti target :( id) aTarget selector :( SEL) aSelector userInfo :( id) userInfo repeats :( BOOL) yesOrNo; TimeInterval: time interval, floating point value. If the value is less than 0, the system defaults to 0.1. This parameter is used to specify whether to trigger a specified method at a specified interval. target: method-acting object such as self selector: Method userInfo: when the timer fails, the specified object retains and releases the timer. Generally, when nilrepeats: YES, the timer is continuously looping until the timer expires or is released. If NO, it will expire after only one loop.
Other methods
-(Void) fire; trigger the timer immediately-(void) invalidate; stop the timer and invalidate the trigger-(BOOL) isValid; Determine whether the trigger is valid-(NSTimeInterval) timeInterval; trigger interval-(id) userInfo; generally nil-(NSDate *) fireDate; // start time-(void) setFireDate :( NSDate *) date; // set fireData, in fact, it will be used for pausing and starting
Brief description of timer pause and start
[Timer setFireDate: [NSDate distantPast]; // enable [timer setFireDate: [NSDate date]; // continue. [Timer setFireDate: [NSDate distantFuture]; // The timer is still valid after the pause is paused. If invalidate is used, the timer becomes invalid.
Before invalidate, it is best to first use isValid to determine whether or not to return to the thread
if ([timer isValid] == YES) { [timer invalidate]; timer = nil;}