做一个100秒的倒计时程序,注意考虑程序进入后台时的情况
2013-07-20 21:25
344 查看
在.h文件中定义:
NSTimer * timer;//计时器
@property (nonatomic,retain) NSTimer *timer;
在.m文件中:
@synthesize timer;
int seconds = 100;//100秒
在didFinishLaunchingWithOptions方法中:
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:YES];//初始化计时器,每个1秒将会调用timerFireMethod方法
-(void)timerFireMethod:(NSTimer *)theTimer{
if(seconds == 1){//当seconds还剩1s的适合停止计时器
[theTimer invalidate];
}else{
seconds --;//实现倒计时功能
NSLog(@"还剩%i秒",seconds);
}
}
在applicationDidEnterBackground中//当应用进入后台时,停止计时
[timer setFireDate:[NSDate distantFuture]];
在applicationWillEnterForeground中//当应用进入前台时,继续计时
[timer setFireDate:[NSDate date]];
ps:由于本人这周才开始正式接触ios,所以有很多地方可能会出现一些bug,欢迎大家指出,相互交流,共同进步
NSTimer * timer;//计时器
@property (nonatomic,retain) NSTimer *timer;
在.m文件中:
@synthesize timer;
int seconds = 100;//100秒
在didFinishLaunchingWithOptions方法中:
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:YES];//初始化计时器,每个1秒将会调用timerFireMethod方法
-(void)timerFireMethod:(NSTimer *)theTimer{
if(seconds == 1){//当seconds还剩1s的适合停止计时器
[theTimer invalidate];
}else{
seconds --;//实现倒计时功能
NSLog(@"还剩%i秒",seconds);
}
}
在applicationDidEnterBackground中//当应用进入后台时,停止计时
[timer setFireDate:[NSDate distantFuture]];
在applicationWillEnterForeground中//当应用进入前台时,继续计时
[timer setFireDate:[NSDate date]];
ps:由于本人这周才开始正式接触ios,所以有很多地方可能会出现一些bug,欢迎大家指出,相互交流,共同进步
相关文章推荐
- 【UI初级 连载二】------做一个100秒倒计时的程序,注意考虑,当程序进入后台时的情况。
- 输入一个整数,将这个整数以字符串的形式逆序输出 程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
- 输入一个整数,将这个整数以字符串的形式逆序输出 程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
- 倒计时封装 程序进入后台不会暂停计时器
- 一个程序在电脑中的运行情况
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序
- 同一个表单可以根据用户的选择,提交给不同的后台处理程序
- 关于QSocket的释放的一个需要注意的情况
- 后台程序的相关注意事项
- ios开发——日常之iOS程序进入后台后仍可运行定时器
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- 编写一个程序,使其从标准输入读取字符,直到遇到0字符(注意是0字符,不是数字)
- 简单说明什么是递归,什么情况下会使用递归,并写一个简单的递归程序。
- js实现随机选取[10,100)中的10个整数,存入一个数组,并排序。 另考虑(10,100]和[10,100]两种情况。
- 打程序应注意的一个问题
- 转:作为一个HR,说说应届生及一两年往届生的注意情况。
- Linux下编译一个静态链接的程序的注意点
- 键盘显示的情况下,按home进入后台,再点击app图标切换回前台时发生crash
- 一个Shift的后门程序,可以让你可以进入你不知道密码的电脑
- Android监听程序进入后台,或者恢复到前台