IOS第六天(2:10秒倒计时)
****************10秒倒计时
#import "HMViewController.h" @interface HMViewController () <UIAlertViewDelegate> @property (weak, nonatomic) IBOutlet UILabel *counterLabel; @property (nonatomic, strong) NSTimer *timer;
@end @implementation HMViewController /** 开始 */
- (IBAction)start
{
// 倒计时10秒,每秒更新一下Label的显示
// 计时器
/**
参数说明
1. 时间间隔,double
2. 监听时钟触发的对象
3. 调用方法
4. userInfo,可以是任意对象,通常传递nil
5. repeats:是否重复
*/
self.counterLabel.text = @""; // scheduledTimerWithTimeInterval 方法本质上就是创建一个时钟,
// 添加到运行循环的模式是DefaultRunLoopMode
// ----------------------------------------------
// 1>
// self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:@"hello timer" repeats:YES]; // ----------------------------------------------
// 2> 与1等价
// self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
// // 将timer添加到运行循环
// // 模式:默认的运行循环模式
// [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode]; // ----------------------------------------------
// 3>
self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
// 将timer添加到运行循环
// 模式:NSRunLoopCommonModes的运行循环模式(监听滚动模式)
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
//[self updateTimer:self.timer];
} /** 时钟更新方法 */
- (void)updateTimer:(NSTimer *)timer
{
// NSLog(@"%s", __func__);
// 1. 取出标签中的数字
int counter = self.counterLabel.text.intValue; // 2. 判断是否为零,如果为0,停止时钟
if (--counter < ) {
// 停止时钟
[self pause]; // 提示用户,提示框
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦......" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"哈哈", nil];
//
// [alert show];
[[[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦......" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"哈哈", nil] show];
} else {
// CTRL + I
// 3. 修改数字并更新UI
self.counterLabel.text = [NSString stringWithFormat:@"%d", counter];
}
} /** 暂停 */
- (IBAction)pause
{
// 停止时钟,invalidate是唯一停止时钟的方法
// 一旦调用了invalidate方法,timer就无效了,如果再次启动时钟,需要重新实例化
[self.timer invalidate];
} #pragma mark - alertView代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ldaaa-------", (long)buttonIndex);
} @end
IOS第六天(2:10秒倒计时)的更多相关文章
- js实现模拟自动点击按钮,并且在10秒倒计时之后疯狂点击
需求来自于csdn问答,可以利用这个原理做秒杀抢单外挂. 代码示例如下: <html> <head> <meta charset="utf-8"/&g ...
- 网页上10秒倒计时,完了后就自动跳转到另一个网页上html代码
用html代码的话就这样: <meta http-equiv="Refresh" content="10;URL=http://www.baidu.com" ...
- js 10秒倒计时 功能
请等待<span id=</span>秒 <script type="text/javascript"> function run(){ var s ...
- iOS “获取验证码”按钮的倒计时功能
iOS 的倒计时有多种实现细节,Cocoa Touch 为我们提供了 NSTimer 类和 GCD 的dispatch_source_set_timer方法去更加方便的使用计时器.我们也可以很容易的的 ...
- js10秒倒计时鼠标点击次数统计
<html> <head> <meta charset="utf-8"/> <script type="text/javascr ...
- js 10秒钟倒计时
第一个: <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- js实现60秒倒计时效果(使用了jQuery)
今天碰到要实现一个类似那种短信验证码60秒倒计时的需求,好久不写js,有点手生.把代码记录下,方便后续查阅. 这里我用了jQuey,毕竟写起来简洁点.下面直接看效果和代码. 一.效果 ...
- iOS 在cell中使用倒计时的处理方法(新)
一.前言 之前的文章iOS 在cell中使用倒计时的处理方法得到大量的支持, 在这先感谢大家的支持. 但是也收到不少人的回复表示不会用, 需要一一解答, 由于之前写的时候没有使用Markdown编辑, ...
- uni验证码60秒倒计时
其实要实现这个功能原理非常简单,就是setInterval+setTimeout+clearInterval结合使用,首先在data里定义一个变量second,初始值为60,然后在setInterva ...
随机推荐
- Servlet获取参数
package action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet ...
- CI批量更新$this->db->update_batch();
$this->db->update_batch(); 生成一条update命令是以你提供的数据为基础的,并执行查询.你可以传递一个数组或对象的参数给update_batch()函数.下面是 ...
- Java Hour 13 集合基础
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. Java 中的集 ...
- php中base64_decode与base64_encode加密解密函数
php中base64_decode与base64_encode加密解密函数,实例分析了base64加密解密函数的具体用法,具有一定的实用价值,需要的朋友可以参考下 本文实例讲述了php中base64_ ...
- (二)WebRTC手记之框架与接口
转自:http://www.cnblogs.com/fangkm/p/4370492.html 转载请注明出处:http://www.cnblogs.com/fangkm/p/4370492.html ...
- String 和 document 的相互转换总结
转自:http://blog.sina.com.cn/s/blog_7f865faf01014qrs.html 一.使用最原始的javax.xml.parsers,标准的jdk api // 字符串转 ...
- js实现快速排序(in-place)简述
快速排序,又称划分交换排序.以分治法为策略实现的快速排序算法. 本文主要要谈的是利用javascript实现in-place思想的快速排序 分治法: 在计算机科学中,分治法是建基于多项分支递归的一种很 ...
- UVALive 6884 GREAT + SWERC = PORTO dfs模拟
题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- loadrunner关联的感悟:
首先用URL模式录制脚本: 切换到树模式下: 很容易查看到关联的位置,由此知道产生动态值得请求为nav.pl 但是在html中我们怎么查看关联值在哪里呢: 今天无意中意识到这个问题: 先看树视图脚本:
- vim 全局替换命令
语法 :[addr]s/源字符串/目的字符串/[option] :%s/源字符串/目的字符串/c 全局替换命令为: :%s/源字符串/目的字符串/g [add ...