基于dispatch_after封装YXTimer

基于dispatch_after封装YXTimer

本人根据dispatch_after封装了一个定时器,支持block以及代理的方式来激活定时器,适用于对精度要求低,耗时短的地方,高端大气上档次,低调奢华有内涵:)

源码:

YXTimer.h 与 YXTimer.m

//
//  YXTimer.h
//  YXTimer
//
//  Created by YouXianMing on 14-10-2.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>
@class YXTimer;

@protocol YXTimerDelegete <NSObject>
- (void)YXTimerEvent:(YXTimer *)timer;
@end

@interface YXTimer : NSObject

// 代理相关方法
@property (nonatomic) NSTimeInterval              milliSecond; // 毫秒
@property (nonatomic, assign) id<YXTimerDelegete> delegate;


// block相关方法
+ (instancetype)timerWithMilliSecondInterval:(NSTimeInterval)milliSecond
                                       Block:(void (^)(YXTimer *timer))block;

// 激活定时器 + 停止定时器
- (void)start;
- (void)stop; // 注意:要想让timer能被移除,一定要激活stop方法才行

@end
//
//  YXTimer.m
//  YXTimer
//
//  Created by YouXianMing on 14-10-2.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#ifdef DEBUG
#define YXTimer_DLog(fmt, ...) NSLog((@"YXTimer.m:%s:%d" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define YXTimer_DLog(...)
#endif

#import "YXTimer.h"

@interface YXTimer ()

@property (nonatomic) BOOL isRunning;
@property (nonatomic, copy) void (^block)(YXTimer *timer);

@end

@implementation YXTimer

- (instancetype)init
{
    self = [super init];
    if (self) {
        _isRunning = NO;
    }
    return self;
}

- (instancetype)initWithBlock:(void (^)(YXTimer *timer))block {
    self = [super init];
    
    if (self) {
        _isRunning = NO;
        _block     = block;
    }
    
    return self;
}

- (void)start {
    _isRunning = YES;
    [self runTimer];
}

- (void)stop {
    _isRunning = NO;
}

+ (instancetype)timerWithMilliSecondInterval:(NSTimeInterval)milliSecond
                                       Block:(void (^)(YXTimer *timer))block {
    YXTimer *timer = [[YXTimer alloc] initWithBlock:block];
    timer.milliSecond = milliSecond;
    
    return timer;
}

- (void)runTimer {
    
    if (self.isRunning) {
        
        NSTimeInterval  milliSecond = 1000.f;
        if (_milliSecond > 0) {
            milliSecond = _milliSecond;
        }
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(milliSecond * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
            if (self.block) {
                self.block(self);
            }
            
            if (_delegate) {
                [_delegate YXTimerEvent:self];
            }
            
            [self runTimer];
        });
    }
}

- (void)dealloc {
    [self stop];
    YXTimer_DLog(@"资源释放了");
}

@end

使用源码:

//
//  ViewController.m
//  YXTimer
//
//  Created by YouXianMing on 14-10-2.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "YXTimer.h"

@interface ViewController ()<YXTimerDelegete>

@property (nonatomic, strong) YXTimer   *timer;
@property (nonatomic, strong) UILabel   *label;
@property (nonatomic, strong) NSArray   *dataArray;
@property (nonatomic)         NSInteger  count;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = @[@"Y.X. Loading .",
                   @"Y.X. Loading ..",
                   @"Y.X. Loading ...",
                   @"Y.X. Loading ....",
                   @"Y.X. Loading .....",
                   @"Y.X. Loading ......",
                   @"Y.X. Loading ......."];
    
    _timer             = [YXTimer new];
    _timer.milliSecond = 100;
    _timer.delegate    = self;
    [_timer start];
    
    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
    _label.textAlignment = NSTextAlignmentLeft;
    _label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.f];
    _label.textColor = [UIColor redColor];
    _label.center = self.view.center;
    [self.view addSubview:_label];
    
}

- (void)YXTimerEvent:(YXTimer *)timer
{
    _label.text = _dataArray[_count++ % _dataArray.count];
}

@end

需要注意的地方:

原文地址:https://www.cnblogs.com/YouXianMing/p/4003907.html