IOS development-NSTimer

Source: Internet
Author: User

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;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.