Initial commit

This commit is contained in:
lianxiang
2018-08-22 11:15:52 +08:00
parent d98e20881f
commit 249bf6864e
491 changed files with 68665 additions and 7 deletions
@@ -0,0 +1,13 @@
//
// GiGaBaseNavViewController.h
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface GiGaBaseNavViewController : UINavigationController
@end
@@ -0,0 +1,68 @@
//
// GiGaBaseNavViewController.m
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaBaseNavViewController.h"
@interface GiGaBaseNavViewController ()<UINavigationControllerDelegate>
@end
@implementation GiGaBaseNavViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.interactivePopGestureRecognizer.delegate = (id)self;
self.view.backgroundColor = UIColor.whiteColor;
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
if (self.viewControllers.count > 1 ) {
//自定义返回按钮
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
backBtn.backgroundColor = [UIColor redColor];
[backBtn addTarget:self action:@selector(backItemAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
viewController.navigationItem.leftBarButtonItem = backItem;
}
}
-(void)backItemAction{
[super popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
@@ -0,0 +1,29 @@
//
// GiGaBaseViewController.h
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface GiGaBaseViewController : UIViewController<UIGestureRecognizerDelegate,UINavigationControllerDelegate>
{
UIView *emptyView;
}
//- (void)setNavigationViewWithBGColor:(UIColor *)bgColor TitleColor:(UIColor *)titleColor;
//- (void)setNavigationViewHidden:(BOOL)hidden;
//- (void)addBackButtonWithImageName:(NSString *)imageName;
//- (void)setBackButtonHidden:(BOOL)hidden;
//- (void)setNavigationViewBottomLineHidden:(BOOL)hidden;
//
//- (void)addRightButtonWithBGImageName:(NSString *)rightImageName action:(SEL)rightAction;
//
//- (void)addRefreshHeaderFooterForTableView:(UITableView *)tableView
// refreshHeaderSelector:(SEL)refreshHeaderSelector
// refreshFooterSelector:(SEL)refreshFooterSelector;
@end
@@ -0,0 +1,44 @@
//
// GiGaBaseViewController.m
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaBaseViewController.h"
@interface GiGaBaseViewController ()
{
// UIColor *navigationBarBGColor; //导航栏背景色
// UIColor *navigationBarTitleColor; //导航栏标题字色
// SEL _rightBtnAction;
// SEL _refreshHeaderSelector;
// SEL _refreshFooterSelector;
}
@end
@implementation GiGaBaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
+41
View File
@@ -0,0 +1,41 @@
//
// GiGaHelper.h
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface GiGaHelper : NSObject
/**
* 获取当前时间 格式 yyyy-MM-dd hh:mm:ss
*/
+(NSString *) getNowTimeString;
/**
* 时间转时间戳
*/
+(long) timeStampWithDate:(NSDate *) timeDate;
/**
* 时间戳转时间
*/
+ (NSString *) dateWithTimeStamp:(long) longValue;
/**
* 根据时间间隔获取未来时间戳
hour 几小时
min 几分钟
second 几秒
*/
+(long)getFutureTimetstamp:(NSUInteger)hour minute:(NSUInteger)min second:(NSUInteger)second;
/**
* NSTimeInterval 转分钟秒
*/
+(NSString *)stringWithNSTimerinterval:(NSTimeInterval)interval;
@end
+72
View File
@@ -0,0 +1,72 @@
//
// GiGaHelper.m
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaHelper.h"
@implementation GiGaHelper
+(NSString *) getNowTimeString{
NSDate *now = [NSDate date];
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
formatDate.dateFormat= @"yyyy-MM-dd hh:mm:ss";
NSString *dayStr = [formatDate stringFromDate:now];
return dayStr;
}
+(long) timeStampWithDate:(NSDate *) timeDate{
long timeStamp = 0;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *timeString = [formatter stringFromDate:timeDate];
NSDate *date = [formatter dateFromString:timeString];
timeStamp = (long)[date timeIntervalSince1970];
return timeStamp;
}
+ (NSString *) dateWithTimeStamp:(long) longValue{
long value = longValue;
NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
formatDay.dateFormat = @"yyyy-MM-dd hh:mm:ss";
NSNumber *time = [NSNumber numberWithLong:value];
//转换成NSTimeInterval
NSTimeInterval nsTimeInterval = [time longValue];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:nsTimeInterval];
NSString *dayStr = [formatDay stringFromDate:date];
return dayStr;
}
+(long)getFutureTimetstamp:(NSUInteger)hour minute:(NSUInteger)min second:(NSUInteger)second{
NSDate *datenow = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY年MM月dd日HH:mm:ss"];
NSTimeInterval timeInterval = hour * 3600 + min * 60 + second;
NSDate *futrueDate = [datenow dateByAddingTimeInterval:timeInterval];
NSString *futrueTimeStr = [formatter stringFromDate:futrueDate];
NSDate *furtureformatDate = [formatter dateFromString:futrueTimeStr];
long futrueTimestamp= [furtureformatDate timeIntervalSince1970];
return futrueTimestamp;
}
+(NSString *)stringWithNSTimerinterval:(NSTimeInterval)interval{
NSInteger min = interval / 60;
NSInteger sec = (NSInteger) interval % 60;
return [NSString stringWithFormat:@"%02ld:%02ld",min,sec];
}
@end
+71
View File
@@ -0,0 +1,71 @@
//
// GiGaAPIRequest.h
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GiGaAPIResult.h"
@class GiGaAPIRequest;
#define DEFALUT_APIREQUEST_TIMEOUT 20
typedef enum GiGaRequestType {
kRequestTypeGet,
kRequestTypePost,
kRequestTypeUpload,
kRequestTypeMultiUpload
}GiGaRequestTyp;
typedef enum ApiResultFormat {
kApiResultJson,
kApiResultXml,
}ApiResultFormat;
@protocol GiGaApiRquestDelegate<NSObject>
@optional
- (void)serverApiFinishedSuccessed:(GiGaAPIRequest *)api result:(GiGaAPIResult *)result;
- (void)serverApiRequestFailed:(GiGaAPIRequest *)api error:(NSError *)error;
- (void)serverApiFinishedFailed:(GiGaAPIRequest *)api result:(GiGaAPIResult *)result;
@end
@interface GiGaAPIRequest : NSObject
#pragma mark - porperty
/** 请求类型 */
@property (nonatomic, readonly) GiGaRequestTyp requestType;
/** 请求返回的格式 */
@property (nonatomic, readonly) ApiResultFormat resultFormat;
/** 请求超时时间 */
@property (nonatomic, readonly) NSTimeInterval timeout;
/** 服务器地址 */
@property (nonatomic, readonly) NSString *serviceUrl;
/** 请求路径 */
@property (nonatomic, readonly) NSString *fullUrl;
/** 代理 */
@property (nonatomic, weak) id<GiGaApiRquestDelegate> delegate;
/** 请求参数数组 */
@property (nonatomic, strong) NSMutableArray *params;
#pragma mark - Method
- (id)initWithDelegate:(id<GiGaApiRquestDelegate>)delegate;
/**
* 拼接公共参数
*/
- (void)appendBaseParams;
#pragma mark - APIRequestDelegate
/**
* 返回数据调用方法
*/
- (void)callBackFinishedWithDictionary:(NSDictionary *)dic;
/**
* 返回数据错误
*/
- (void)callBackFailed:(NSError *)error;
@end
+81
View File
@@ -0,0 +1,81 @@
//
// GiGaAPIRequest.m
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaAPIRequest.h"
#import "GiGaServerConfig.h"
@implementation GiGaAPIRequest
- (id)initWithDelegate:(id<GiGaApiRquestDelegate>)delegate
{
if (self = [super init]) {
self.params = [NSMutableArray array];
self.delegate = delegate;
}
return self;
}
//默认Get访问
- (GiGaRequestTyp)requestType
{
return kRequestTypeGet;
}
//默认超时时间
- (NSTimeInterval)timeout
{
return DEFALUT_APIREQUEST_TIMEOUT;
}
//服务器地址
-(NSString *)serviceUrl{
return [GiGaServerConfig getMainUrl];
}
/** 拼接的请求地址 */
//
- (NSString *)fullUrl
{
// 服务器地址 和 接口名
NSString *url = [NSString stringWithFormat:@"%@",self.serviceUrl];
return url;
}
// 数据请求完成的 回调
- (void)callBackFinishedWithDictionary:(NSDictionary *)dic
{
// 处理 responseObject
GiGaAPIResult *resu = [[GiGaAPIResult alloc] initWithDictionary:dic];
// error ID = 0
if (resu.success) {
if (self.delegate && [self.delegate respondsToSelector:@selector(serverApiFinishedSuccessed: result:)]) {
//
[self.delegate serverApiFinishedSuccessed:self result:resu];
}
// error ID = 1
} else {
if (self.delegate && [self.delegate respondsToSelector:@selector(serverApiFinishedFailed:result:)]) {
[self.delegate serverApiFinishedFailed:self result:resu];
}
}
}
// 数据请求失败的回调
- (void)callBackFailed:(NSError *)error
{
if (self.delegate && [self.delegate respondsToSelector:@selector(serverApiRequestFailed:error:)]) {
[self.delegate serverApiRequestFailed:self error:error];
}
}
// 拼接的基本参数
- (void)appendBaseParams
{
}
@end
+24
View File
@@ -0,0 +1,24 @@
//
// GiGaAPIResult.h
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface GiGaAPIResult : NSObject
/** 提示信息 */
@property (nonatomic, copy) NSString *message;
/** 请求状态 */
@property (nonatomic, assign) NSInteger status;
/** 请求是否成功 */
@property (nonatomic, readonly) BOOL success;
/** 接收数据的字典 */
@property (nonatomic, strong) NSDictionary *dic;
- (id)initWithDictionary:(NSDictionary *)dic;
@end
+49
View File
@@ -0,0 +1,49 @@
//
// GiGaAPIResult.m
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaAPIResult.h"
@implementation GiGaAPIResult
-(BOOL)success{
return self.status == 0;
}
-(id)initWithDictionary:(NSDictionary *)dic
{
if (self = [super init]) {
@try {
if (dic && ![dic isKindOfClass:[NSNull class]]) {
self.status = [[dic objectForKey:@"errorcode"] intValue];//待定
self.message = [dic objectForKey:@""];//待定
NSDictionary *data = [[dic objectForKey:@"result"] objectForKey:@"data"];
self.dic = data;
}else{
self.message = @"网络错误";
self.dic = nil;
self.status = 1;
}
}
@catch(NSException *exception) {
self.dic = dic;
self.status = 0;
}
@finally {
}
}
return self;
}
@end
+15
View File
@@ -0,0 +1,15 @@
//
// GiGaNetManager.h
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GiGaAPIRequest.h"
@interface GiGaNetManager : NSObject
+(GiGaNetManager *)shareInstance;
+(void)request:(GiGaAPIRequest*)api;
@end
+70
View File
@@ -0,0 +1,70 @@
//
// GiGaNetManager.m
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaNetManager.h"
#import "AFNetworking.h"
#import "GiGaServerConfig.h"
@interface GiGaNetManager()
@end
@implementation GiGaNetManager
+ (GiGaNetManager *)shareInstance{
static GiGaNetManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
+ (void)request:(GiGaAPIRequest *)api{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURL *baseUrl = [NSURL URLWithString:[GiGaServerConfig getMainUrl]];
AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc] initWithBaseURL:baseUrl sessionConfiguration:configuration];
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
switch (api.requestType) {
case kRequestTypeGet:
{
[manager GET:api.fullUrl parameters:api.params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *outDic = (NSDictionary *)responseObject;
[api callBackFinishedWithDictionary:outDic];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[api callBackFailed:error];
}];
}
break;
case kRequestTypePost:
{
[manager POST:api.fullUrl parameters:api.params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *outDic = (NSDictionary *)responseObject;
[api callBackFinishedWithDictionary:outDic];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[api callBackFailed:error];
}];
}
break;
default:
break;
}
}
@end
+42
View File
@@ -0,0 +1,42 @@
//
// GiGaUserDefault.h
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface GiGaUserDefault : NSObject
+(void)saveUserId:(NSString *)userId;
+(NSString *)getCurentUserId;
+(void)removeUserId;
+(BOOL)isUserLogin;
/**
保存用户表版本号
*/
+(void)saveDBVersion:(NSInteger)versionNum;
+(NSInteger)getOldVersion;
/**
新手页(蒙版)
@param showedGaurd 是否展示过 default NO
*/
+(void)saveUsergaurdflag:(BOOL)showedGaurd;
+(BOOL)isShowedGaurd;
/**
App 引导页
@param showedGaurd 是否展示过 default NO
*/
+(void)saveAppGaurdflag:(BOOL)showedGaurd;
+(BOOL)isShowedAppGaurd;
@end
+79
View File
@@ -0,0 +1,79 @@
//
// GiGaUserDefault.m
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaUserDefault.h"
NSString *const kUserManagerVerision = @"GiGAUserDBVeriosn";
NSString *const GIGADEFAULTKEY_USERID = @"GiGaUserId";
NSString *const kShowUserGuard = @"ShowUserGuard";
NSString *const kShowAppGuard = @"ShowAppGuard";
@implementation GiGaUserDefault
+(void)saveUserId:(NSString *)userId{
UD_SET_KEY_VALUE(GIGADEFAULTKEY_USERID, userId);
}
+(NSString*)getCurentUserId{
NSString *userid = UD_GET_VALUE(GIGADEFAULTKEY_USERID);
return userid;
}
+(void)removeUserId{
UD_REMOVE_KEY(GIGADEFAULTKEY_USERID);
}
+(BOOL)isUserLogin{
NSString *uid = UD_GET_VALUE(GIGADEFAULTKEY_USERID);
return uid ? YES : NO;
}
+(void)saveDBVersion:(NSInteger)versionNum{
NSNumber *verNum = [NSNumber numberWithInteger:versionNum];
UD_SET_KEY_VALUE(verNum.stringValue, kUserManagerVerision);
}
+ (NSInteger)getOldVersion{
return (NSInteger)UD_GET_VALUE(kUserManagerVerision);
}
+(void)saveUsergaurdflag:(BOOL)showedGaurd
{
[UD_STADARDUD setBool:showedGaurd forKey:kShowUserGuard];
[UD_STADARDUD synchronize];
}
+(BOOL)isShowedGaurd
{
BOOL showedGaurd = [UD_STADARDUD boolForKey:kShowUserGuard];
return showedGaurd;
}
+(void)saveAppGaurdflag:(BOOL)showedGaurd;
{
[UD_STADARDUD setBool:showedGaurd forKey:kShowAppGuard];
[UD_STADARDUD synchronize];
}
+(BOOL)isShowedAppGaurd{
BOOL showedGaurd = [UD_STADARDUD boolForKey:kShowAppGuard];
return showedGaurd;
}
@end
+25
View File
@@ -0,0 +1,25 @@
//
// GiGaUserManager.h
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GiGaUser.h"
@interface GiGaUserManager : NSObject
@property(nonatomic,strong) GiGaUser *user;
+(instancetype)shareUser;
- (void)saveUser:(GiGaUser *)user;
- (GiGaUser*)getCurrentUser;
-(void)loginOut;
/**
版本升级检测
*/
//-(void)detectionUserUpgrade;
@end
+108
View File
@@ -0,0 +1,108 @@
//
// GiGaUserManager.m
// GIGA
//
// Created by lianxiang on 2018/8/15.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaUserManager.h"
#import "YTKKeyValueStore.h"
#import "GiGaUserDefault.h"
NSString *const GIGA_USER_TABLE = @"user_table";
const static NSInteger GIGADB_USER_VER = 0;//当前数据库版本号
@interface GiGaUserManager()
@property (nonatomic,strong) YTKKeyValueStore *store;
@property (nonatomic,strong) NSMutableArray *userArr;
@property (nonatomic,copy) NSString *userTabId;
@end
@implementation GiGaUserManager
+ (instancetype)shareUser{
static GiGaUserManager *userManager = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
userManager = [[self alloc] init];
});
return userManager;
}
-(instancetype)init{
self = [super init];
if (self) {
// 打开名为GiGa.db的数据库,如果该文件不存在,则创新一个新的。
self.store = [[YTKKeyValueStore alloc] initDBWithName:@"GiGa.db"];
[self creatTableByName:GIGA_USER_TABLE];
_userArr = [NSMutableArray new];
[self detectionUserUpgrade];
}
return self;
}
//建表
-(void)creatTableByName:(NSString *)tableName{
[_store createTableWithName:tableName];
}
- (void)saveUser:(GiGaUser *)user{
self.user = user;
NSString *userTableId = [NSString stringWithFormat:@"%@",user.userId];
self.userTabId = userTableId;
//暂定只保留一个用户
[_userArr removeAllObjects];
[_userArr addObject:user];
[_store putObject:_userArr withId:userTableId intoTable:GIGA_USER_TABLE];
[GiGaUserDefault saveUserId:userTableId];
}
//已登录
-(GiGaUser *)getCurrentUser{
NSString *useriD = [GiGaUserDefault getCurentUserId];
NSArray *usrA= [_store getObjectById:useriD fromTable:GIGA_USER_TABLE];
if (usrA && usrA.count > 0) {
self.user = usrA[0];
}
return self.user;
}
//
-(void)loginOut{
self.user = nil;
[GiGaUserDefault removeUserId];
}
- (void)detectionUserUpgrade{
NSInteger oldVerion = [GiGaUserDefault getOldVersion];
if (GIGADB_USER_VER <= oldVerion) {
return;
}
[self upgradUserDBToNew];
[GiGaUserDefault saveDBVersion:GIGADB_USER_VER];
}
-(void)upgradUserDBToNew{
if (_userArr.count <= 0) {
GILog(@"there is no user yet");
return;
}
[_store putObject:_userArr withId:self.userTabId intoTable:GIGA_USER_TABLE];
}
@end
+30
View File
@@ -0,0 +1,30 @@
//
// LXCountTimer.h
// Elastagen
//
// Created by lianxiang on 2018/8/12.
// Copyright © 2018年 jijia. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface LXCountTimer : NSObject
/**
* 用时间戳倒计时
* starTimeStamp 开始的时间戳
* finishTimeStamp 结束的时间戳
*/
-(void)countDownWithStratTimeStamp:(long)starTimeStamp finishTimeStamp:(long)finishTimeStamp completeBlock:(void (^)(NSInteger day,NSInteger hour,NSInteger minute,NSInteger second))completeBlock;
/**
* 每秒走一次,回调block
*/
-(void)countDownWithPER_SECBlock:(void (^)(void))PER_SECBlock;
/**
* 销毁倒计时
*/
-(void)destoryTimer;
@end
+93
View File
@@ -0,0 +1,93 @@
//
// LXCountTimer.m
// Elastagen
//
// Created by lianxiang on 2018/8/12.
// Copyright © 2018年 jijia. All rights reserved.
//
#import "LXCountTimer.h"
@interface LXCountTimer()
@property(nonatomic,retain) dispatch_source_t timer;
@property(nonatomic,retain) NSDateFormatter *dateFormatter;
@end
@implementation LXCountTimer
-(void)countDownWithStratTimeStamp:(long)starTimeStamp finishTimeStamp:(long)finishTimeStamp completeBlock:(void (^)(NSInteger, NSInteger, NSInteger, NSInteger))completeBlock{
if (_timer == nil) {
NSDate *finishDate =[self dateWithLong:finishTimeStamp];
NSDate *startDate = [self dateWithLong:starTimeStamp];
NSTimeInterval timerInterval =[finishDate timeIntervalSinceDate:startDate];
__block int timeout = timerInterval;
if (timeout !=0) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);//每秒执行
dispatch_source_set_event_handler(_timer, ^{
if (timeout<=0) {
//记时结束, 关闭
dispatch_source_cancel(self->_timer);
self->_timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
completeBlock(0,0,0,0);
});
}else{
int days = (int)(timeout/(3600*24));
int hours = (int)((timeout-days*24*3600)/3600);
int minute = (int)(timeout-days*24*3600-hours*3600)/60;
int second = timeout-days*24*3600-hours*3600-minute*60;
dispatch_async(dispatch_get_main_queue(), ^{
completeBlock(days,hours,minute,second);
});
timeout--;
}
});
dispatch_resume(_timer);
}
}
}
-(void)countDownWithPER_SECBlock:(void (^)(void))PER_SECBlock{
if (_timer==nil) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
PER_SECBlock();
});
});
dispatch_resume(_timer);
}
}
-(void)destoryTimer{
if (_timer) {
dispatch_source_cancel(_timer);
_timer = nil;
}
}
-(void)dealloc{
NSLog(@"%s dealloc",object_getClassName(self));
}
-(NSDate *)dateWithLong:(long)longValue{
long value = longValue;
NSNumber *time = [NSNumber numberWithLong:value];
//转换成NSTimeInterval
NSTimeInterval nsTimeInterval = [time longValue];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:nsTimeInterval];
return date;
}
@end
File diff suppressed because one or more lines are too long