您的位置:首页 > 移动开发 > IOS开发

iOS 定时器封装

2015-08-28 15:02 375 查看
支持启动多个定时器,关闭的时候要记住回调出来的TimeModle的名字

#import <Foundation/Foundation.h>

#import "BLAppDelegate.h"

typedef
void(^TimeManagerCallback)(BOOL success,
id result);

@interface TimeManager :
NSObject

/*

name 唯一标示

num 超时时间

*/
+ (void)initTime:(int)num callback:(TimeManagerCallback)callback;
+ (void)closeTime:(NSString *)name;
+ (void)closeTime;

/*

例子

- (void)close:(UIButton *)button{

[TimeManager closeTime:@"BLLogin"];

}

- (void)login:(UIButton *)button

{

[TimeManager initTime:@"BLLogin" timeout:30 callback:^(BOOL success, id result){

if (success) {

NSLog(@"success");

}

}];

}

*/

@end

//

// FBManager.m

// FBManager

#import "TimeManager.h"

#import "TimeModle.h"

@interface
TimeManager()

@property (nonatomic ,strong)NSMutableArray *timeArray;

@property (nonatomic ,strong)NSString *name;

+ (TimeManager *)shared;

@end

@implementation TimeManager

#pragma mark -

#pragma mark - Singleton

+ (TimeManager *)shared
{

static TimeManager *scTime =
nil;

@synchronized (self){

static dispatch_once_t pred;

dispatch_once(&pred, ^{
scTime = [[TimeManager
alloc] init];
scTime.timeArray = [NSMutableArray
new];
});
}

return scTime;
}

#pragma mark -

#pragma mark - Public Methods

+ (void)initTime:(int)num callback:(TimeManagerCallback)callback;
{
[[TimeManager
shared] initTime:num
callback:callback];
}

+ (void)closeTime:(NSString *)name{
[[TimeManager
shared] closeTime:name];
}
+ (void)closeTime{

[[TimeManager
shared] closeTime];
}

#pragma mark -

#pragma mark - Private Methods

- (void)initTime:(int)num callback:(TimeManagerCallback)callback{

TimeModle *item = [[TimeModle
alloc] initWithBlock:callback];

item.num = num;
item.name = [[TimeManager
shared] getUniqueCode];

self.name = item.name;
[self.timeArray
addObject:item];
}

- (void)closeTime:(NSString *)name{

for (TimeModle *item
in self.timeArray) {

if ([item.name
isEqualToString:name]) {
[item.timeTimer
invalidate];
item.timeTimer =
nil;
item.name =
nil;
[self.timeArray
removeObject:item];

break;
}
}
}
- (void)closeTime{

for (TimeModle *item
in self.timeArray) {

if ([item.name
isEqualToString:self.name]) {
[item.timeTimer
invalidate];
item.timeTimer =
nil;
item.name =
nil;
[self.timeArray
removeObject:item];

break;
}
}
}

- (NSString *)getUniqueCode
{

NSString *uniqueId =
@"";

CFUUIDRef uuidRef =CFUUIDCreate(NULL);

CFStringRef uuidStringRef =CFUUIDCreateString(NULL, uuidRef);

CFRelease(uuidRef);

uniqueId = (__bridge
NSString *)uuidStringRef;

return uniqueId;
}

@end

//

//

#import <Foundation/Foundation.h>

#import "TimeManager.h"

@interface TimeModle :
NSObject
{

TimeManagerCallback _callback;

}

- (id)initWithBlock:(TimeManagerCallback)callback;

@property (nonatomic,
retain) NSString * name;

@property (assign)
int num;

@property (nonatomic ,strong)NSTimer * timeTimer;

- (void)loginTimeout;

@end

#import "TimeModle.h"

@implementation TimeModle

@synthesize name;
@synthesize timeTimer;

//- (id) init

//{

// if (self = [super init])

// {

// self.timeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(loginTimeout:) userInfo:nil repeats:YES];

// [[NSRunLoop currentRunLoop] addTimer:self.timeTimer forMode:NSRunLoopCommonModes];

// }

// return self;

//}

- (id)initWithBlock:(TimeManagerCallback)callback{

self = [super
init];

if (self) {

if (callback) {

_callback = callback;

self.timeTimer = [NSTimer
scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(loginTimeout)
userInfo:nil
repeats:YES];

[[NSRunLoop
currentRunLoop] addTimer:self.timeTimer
forMode:NSDefaultRunLoopMode];
}

}

return
self;

}

- (void)loginTimeout
{

self.num -- ;

NSLog(@"self.name = %@ num = %d",self.name,self.num);

if (self.num <=
0) {

_callback(YES,self.name);
}else
{

_callback(NO,self.name);
}

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: