倒计时封装 程序进入后台不会暂停计时器
来源:互联网 发布:2016欧洲杯数据 编辑:程序博客网 时间:2024/05/21 10:43
.h
/**
时间改变时执行
@param timeArray天,时,分,秒数组
*/
typedefvoid(^TimeChangeBlock)(NSMutableArray *timeArray);
.m
/**
倒计时
@param endTimeStep倒计时一共有多少时间(秒)
@param timeChange每次时间改变时执行
@param timeEnd时间归0是执行
*/
- (void)countDownTime:(NSInteger )endTimeStep withTimeChange:(TimeChangeBlock)timeChange withTimeEnd:(TimeChangeBlock)timeEnd{
__blockNSInteger timeout = endTimeStep/1000;//倒计时时间
//队列(队列时用来确定该定时器存在哪个队列中)
dispatch_queue_t queue =dispatch_get_main_queue();
//创建GCD定时器
dispatch_source_t timer =dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);
// _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_time_t start =dispatch_time(DISPATCH_TIME_NOW,0 *NSEC_PER_SEC);//开始时间
uint64_t interval =1 *NSEC_PER_SEC;//时间间隔
//设置GCD定时器开始时间,间隔时间
dispatch_source_set_timer(timer, start, interval,0);
// GCD定时器处理回调方法
dispatch_source_set_event_handler(timer, ^{
NSInteger days = timeout/(3600*24);
NSInteger hours = (timeout-days*24*3600)/3600;
NSInteger minute = (timeout-days*24*3600-hours*3600)/60;
NSInteger second = timeout-days*24*3600-hours*3600-minute*60;
NSMutableArray *timeArray = [NSMutableArrayarrayWithCapacity:0];
[timeArrayaddObject:[NSNumbernumberWithInteger:days]];
[timeArrayaddObject:[NSNumbernumberWithInteger:hours]];
[timeArrayaddObject:[NSNumbernumberWithInteger:minute]];
[timeArrayaddObject:[NSNumbernumberWithInteger:second]];
if (timeChange) {
timeChange(timeArray);
}
timeout--;
if (timeout<0) {//时间归0
//关闭定时器
dispatch_cancel(timer);
if (timeEnd) {
timeEnd(timeArray);
}
}
});
// GCD定时器启动,默认是关闭的
dispatch_resume(timer);
}
appdelegate.m
#pragma mark -程序进入后台后继续倒计时功能
//程序进入后台不会影响到计时功能
- (void)beginBackGroundTask:(UIApplication *)application {
self.bgIdentifier =[application beginBackgroundTaskWithExpirationHandler:^(void) {
//当应用程序留给后台的时间快要到结束时(应用程序留给后台执行的时间是有限的),这个Block块将被执行
//我们需要在次Block块中执行一些清理工作。
//如果清理工作失败了,那么将导致程序挂掉
//清理工作需要在主线程中用同步的方式来进行
dispatch_queue_t mainQueue =dispatch_get_main_queue();
AppDelegate *weakSelf =self;
dispatch_async(mainQueue, ^(void) {
AppDelegate *strongSelf = weakSelf;
if (strongSelf !=nil){
[selfendBackGroundTask];
}
});
}];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[superapplicationDidEnterBackground:application];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[selfbeginBackGroundTask:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[superapplicationWillEnterForeground:application];
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
//application.applicationIconBadgeNumber = 0;
[selfendBackGroundTask];
}
//结束程序进入后台的任务
- (void)endBackGroundTask {
//每个对 beginBackgroundTaskWithExpirationHandler:方法的调用,必须要相应的调用 endBackgroundTask:方法。这样,来告诉应用程序你已经执行完成了。
//也就是说,我们向 iOS 要更多时间来完成一个任务,那么我们必须告诉 iOS 你什么时候能完成那个任务。
//也就是要告诉应用程序:“好借好还”嘛。
//标记指定的后台任务完成
[[UIApplicationsharedApplication]endBackgroundTask:self.bgIdentifier];
//销毁后台任务标识符
self.bgIdentifier = UIBackgroundTaskInvalid;
}
阅读全文
0 0
- 倒计时封装 程序进入后台不会暂停计时器
- 做一个100秒的倒计时程序,注意考虑程序进入后台时的情况
- swift 计时器进入后台后计时问题
- 我的程序已经处于后台运行,从桌面点进入会重新创建,而从任务管理点进入不会?
- swift程序进入后台
- /**倒计时计时器**/
- 倒计时计时器
- 倒计时 计时器
- 倒计时计时器
- CountDownTimer 倒计时封装(可 暂停 继续 重新开始)
- SufaceView播放视频 进入后台重新进入不会播放
- 【UI初级 连载二】------做一个100秒倒计时的程序,注意考虑,当程序进入后台时的情况。
- soc_linux- 前台后台运行程序,后台程序暂停
- Timer 计时器进入后台仍能继续运行
- Android监听程序进入后台
- 程序进入后台运行NSTimer
- 短信发送倒计时按钮封装,退出再进入可继续倒计时,绝对时间倒计时
- android计时器暂停问题
- getchar
- 解决Docker启动服务器链接失败-Job for docker.service failed because the control process exited error code
- Map的value最大长度值
- 递归实现费氏数列:0,1,1,2,3,5,8,13,21,34,55,89,... ...
- java通过JDBC链接SQLServer2012
- 倒计时封装 程序进入后台不会暂停计时器
- React Native入门(十一)之屏幕适配
- Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.
- 【KMP】【poj3167】【bzoj1729】Cow Patterns (kmp变形)
- Excel上传示例及后台解析Excel
- 学习Java8--Lambda表达式
- Spark 编程指南(一)
- 论文代发过程注意哪些事项
- 基于802.11协议的节能技术