Skip to content

基于Dispatch_source_t封装的计时器。直接内核处理,不依赖runloop,受CPU影响极小

License

Notifications You must be signed in to change notification settings

markhetao/HTTimer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
ht
Nov 9, 2020
061d1e8 · Nov 9, 2020

History

1 Commit
Nov 9, 2020
Nov 9, 2020
Nov 9, 2020
Nov 9, 2020
Nov 9, 2020
Nov 9, 2020

Repository files navigation

HTTimer

Example

  • 单例自动启动
- (void)sharedAutoRunDemo {
    
    //单例模式
    __block double currentTime = 0;
    [HTTimer sharedStartWithDuration:3 Repeats:true TimeOverCallBlock:^{
        NSLog(@"已完成");
    } TimeInCallBlock:^{
        NSLog(@"%.0f", currentTime);
        currentTime ++;
    }];
}
  • 单例手动启动
- (void)sharedCustomRunDemo{
    
    // 总时长
    [HTTimer sharedInstance].duration = 3;
    
    // 重复
//    [HTTimer sharedInstance].repeats = false;
    
    __block double currentTime = 0;
    
    // 每次间隔回调
    [HTTimer sharedInstance].timeInCallBlock = ^{
        NSLog(@"%.0f", currentTime);
        currentTime ++;
    };
    
    // 结束回调
    [HTTimer sharedInstance].timeOverCallBlock = ^{
        NSLog(@"已完成");
    };
    
    [[HTTimer sharedInstance] start];
}
  • 自定义对象模式(需要持有timer对象,确保生命周期)
- (void)customDemo {
    
    __block double currentTime = 0;
    
    // 0.5秒间隔 3秒总时长 不重复
    self.timer = [HTTimer initWith:0.5 Duration:3 Repeats:false TimeOverCallBlock:^{
        NSLog(@"已完成");
    } TimeInCallBlock:^{
        NSLog(@"%.0f", currentTime);
        currentTime ++;
    }];

    [self.timer start];
}

Requirements

Installation

HTTimer is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'HTTimer'

Author

markhetao@sina.com

License

HTTimer is available under the MIT license. See the LICENSE file for more info.

About

基于Dispatch_source_t封装的计时器。直接内核处理,不依赖runloop,受CPU影响极小

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published