NSTimer timer summary, nstimer timer Summary
I. Initialization Methods: There are five initialization methods:
+ (NSTimer *) timerWithTimeInterval :( NSTimeInterval) ti invocation :( NSInvocation *) invocation repeats :( BOOL) yesOrNo;
Usage:
-(Void) viewDidLoad {
[Super viewDidLoad];
// Initialize an Invocation object
NSInvocation * invo = [NSInvocation invocationWithMethodSignature: [self class] instanceMethodSignatureForSelector: @ selector (init)];
[Invo setTarget: self];
[Invo setSelector: @ selector (myLog)];
NSTimer * timer = [NSTimer timerWithTimeInterval: 1 invocation: invo repeats: YES];
// Add to the main cyclic pool
[[Nsunloop mainRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
// Start the loop
[Timer fire];
}
+ (NSTimer *) scheduledTimerWithTimeInterval :( NSTimeInterval) ti invocation :( NSInvocation *) invocation repeats :( BOOL) yesOrNo;
Usage:
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 1 invocation: invo repeats: YES];
+ (NSTimer *) timerWithTimeInterval :( NSTimeInterval) ti target :( id) aTarget selector :( SEL) aSelector userInfo :( id) userInfo repeats :( BOOL) yesOrNo;
Usage:
NSTimer * timer = [NSTimer timerWithTimeInterval: 1 target: self selector: @ selector (myLog) userInfo: nil repeats: NO]
+ (NSTimer *) scheduledTimerWithTimeInterval :( NSTimeInterval) ti target :( id) aTarget selector :( SEL) aSelector userInfo :( id) userInfo repeats :( BOOL) yesOrNo;
Usage:
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (myLog :) userInfo: @ "123" repeats: YES]
-(Instancetype) initWithFireDate :( NSDate *) date interval :( NSTimeInterval) ti target :( id) t selector :( SEL) s userInfo :( id) ui repeats :( BOOL) rep
Usage:
NSTimer * timer = [[NSTimer alloc] initWithFireDate: [NSDate distantPast] interval: 1 target: self selector: @ selector (myLog :) userInfo: nil repeats: YES];
[[Nsunloop mainRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
Note: the differences between the five initialization methods are as follows:
1. The repeats parameter specifies whether to execute cyclically. YES indicates that the loop is executed, and NO indicates that the loop is executed only once.
2. If you do not need to manually add the addTimer: forMode Method to the master loop pool, timerWithTimeInterval will not be executed cyclically. If you do not manually call fair, the timer will not start.
3. The scheduledTimerWithTimeInterval method does not need to call fair manually. It is automatically executed and automatically added to the master loop pool.
4. The init method needs to be manually added to the loop pool, which will be started at the set start time.
Ii. member variables
@ Property (copy) NSDate * fireDate;
This is to set the start time of the timer. It is often used to manage the start and stop of the timer.
// Start the timer
Timer. fireDate = [NSDate distantPast];
// Stop the timer
Timer. fireDate = [NSDate distantFuture];
@ Property (readonly) NSTimeInterval timeInterval;
This is a read-only attribute that gets the timer call interval.
@ Property NSTimeInterval tolerance;
This is an attribute added after 7.0, because NSTimer is not completely accurate, and the error range is set through this value.
@ Property (readonly, getter = isValid) BOOL valid;
Get whether the timer is valid
@ Property (readonly, retain) id userInfo;
Get parameter information
Iii. Memory release
If we start a timer, it is not enough to stop the timer before it is released on a certain interface because the system still keeps this object in the loop pool. So we need to do this:
-(Void) dealloc {
NSLog (@ "dealloc: % @", [self class]);
}
-(Void) viewDidLoad {
[Super viewDidLoad];
Timer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (myLog :) userInfo: nil repeats: YES];
UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (0, 0,100,100)];
Btn. backgroundColor = [UIColor redColor];
[Btn addTarget: self action: @ selector (btn) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: btn];
}
-(Void) btn {
If (timer. isValid ){
[Timer invalidate];
}
Timer = nil;
[Self dismissViewControllerAnimated: YES completion: nil];
}
In the official document, we can see that [timer invalidate] is the only way to remove the timer from the loop pool.