-
GCD
@interface ViewController ()
@property (nonatomic, strong) dispatch_source_t timer;
@end
//get main queue
dispatch_queue_t queue = dispatch_get_main_queue();
// init a timer (substantially dispatch_source_t is an object of NSObject)
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//@param DISPATCH_TIME_NOW start time
//@param int64_t time argument (nanosecond)
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
uint64_t interval = (uint64_t)(0.1 * NSEC_PER_SEC);
dispatch_source_set_timer(self.timer, start, interval, 0);
// set call-back
dispatch_source_set_event_handler(self.timer, ^{
[NSLog(@"6666")];
});
//start timer
dispatch_resume(self.timer);
-
CADisplayLink
1.init
CADisplayLink * displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(method)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
displayLink.preferredFramesPerSecond = 6;
2. stop
When the CADisplayLink object is added to the runloop, the selector can be called periodically, similar to the repeated NSTimer being started; When an invalidate operation is performed, the CADisplayLink object is removed from the runloop, and the selector call stops, similar to the NSTimer invalidate method.
[displayLink invalidate];
displayLink = nil;
3.feature
Called while the screen refresh: CADisplayLink is a timer class that lets us draw specific content to the screen at the same frequency as the screen refresh rate. After registration to the runloop CADisplayLink in a particular pattern, whenever the end of the screen display content to refresh, to the CADisplayLink runloop specified target to send a specified selector, CADisplayLink class matching the selector will be called again. So typically, the refresh rate of the iOS device screen is 60 beats per second.
Latency: the screen refresh rate for iOS devices is fixed, and CADisplayLink will be called at the end of each refresh in normal circumstances, with a high degree of accuracy. However, if the method is time-consuming and exceeds the screen refresh cycle, it will result in skipping a number of callback opportunities.
If the CPU is too busy to ensure a refresh rate of 60 times per second, the chance of skipping a number of calls to callback methods is skipped, and the number of times the CPU is busy.
Usage scenario: it can be seen from the principle that CADisplayLink is suitable for continuous redrawing of the interface. For example, when video is playing, it needs to continuously get the next frame for rendering the interface.
important property
frameInterval (deprecated after iOS 10.0 instead by preferredFramesPerSecond )
The value of the NSInteger type, which is used to set the interval how many frames call a selector method, and the default value is 1, which is called once per frame.
duration
The CFTimeInterval value of readOnly represents the time interval between two screen refresh. It is important to note that this property will not be assigned until the target selector is first invoked. The call interval time for the selector is calculated by calling the interval time = duration * frameInterval.
-
NSTimer(Too simple to explain.)
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(method)
userInfo:nil
repeats:YES];
网友评论