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
#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
相关文章推荐
- 【转载】 聊聊 iOS 开发
- ios 基于AFN的图片上传
- iOS开发之多线程(线程间的通信)
- iOS开发:创建真机调试证书
- ios--本地通知
- iOS中的结构体保存到数组与读取
- ios 富文本
- 【iOS】有用资源
- nagios监控脚本编写
- IOS消息转发机制
- iOS开发-loadView和viewDidLoad总结
- iOS 本地推送 (本地通知)
- iOS FMDB小试了一下
- iOS开发之多线程(NSThread创建线程)
- ios禁用多按钮同时点下的效果
- iOS 大型项目开发漫谈
- iOS下的通知使用
- ios开发之 苹果审核被拒,使用第三方登录,但手机没有安装客户端的平台
- iOS开发之 二维码生成/二维码扫描
- ios开发之 icon规范+启动图规范+欢迎页规范