AJ分享,必须精品

先看效果




代码



#import "NYViewController.h"

@interface NYViewController () <UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *counterLabel;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation NYViewController

/**开始*/
-(IBAction)start{
// 倒计时10秒,计时器
/* NSTimer scheduledTimerWithTimeInterval
参数说明:
1,时间间隔,double
2,监听时钟触发的对象
3,调用方法
4,userInfo,可以是任意对象,通常传递nil,如果有传递值,大多数是为了在多个计数器中分辨用哪个
5,repeats:是否重复执行调用方法。
*/ // scheduledTimerWithTimeInterval 方法本质上就是创建一个时钟,
// 添加到运行循环的模式是DefaultRunloopMode
// _________________________________________________________________________________
//1>
// self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil 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]; } /**每秒更新counterLabel属性*/
-(void) updateTimer
{
//1,取出标签中得数字
int counter = self.counterLabel.text.intValue;
//2,判断是否为0,如果是则停止时钟
if (--counter<0) {
//提示用户,提示框
[[[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦。。。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
//AlertView 中输入的最后是数组,可以通过代理方式来实现方法
[self pause]; }else{
//,3修改数字并显示更新UILabel
self.counterLabel.text = [NSString stringWithFormat:@"%d",counter];
} } /**暂停*/
-(IBAction)pause
{
//停止时钟,invalidate是唯一的方法,一调用就干掉timer了,想再用只能重新实例化
[self.timer invalidate];
} -(IBAction)stop
{
[self pause];
self.counterLabel.text = @"10";
} #pragma mark - alertView 代理方法 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{ NSLog(@"%d",buttonIndex);
} @end

注意点NSTimer

用法:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]
参数说明:
1,时间间隔,double
2,监听时钟触发的对象
3,调用方法
4,userInfo,可以是任意对象,通常传递nil,如果有传递值,大多数是为了在多个计数器中分辨用哪个
5,repeats:是否重复执行调用方法。

是否要在发生滚动事件时候继续计时器
将timer添加到运行循环,模式:NSRunLoopCommonModes监听滚动模式
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];

提示框 UIAlertView

提示框
[[[UIAlertView alloc] initWithTitle:@”开始” message:@”开始啦。。。” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil] show];
AlertView 中输入的最后是数组,可以通过代理方式来实现方法

#pragma mark - alertView 代理方法

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{ NSLog(@"%d",buttonIndex);
//0指的是取消按钮
//可以加入if判断buttonIndx为多少来加入事件
}

AJ学IOS(10)UI之_NSTimer_ios计时器的更多相关文章

  1. AJ学IOS(28)UI之Quartz2D简单介绍

    AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...

  2. AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引

    AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...

  3. AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController

    AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...

  4. AJ学IOS(01) UI之Hello World与加法计算器

    不多说,AJ分享,必须精品 这两个一个是HelloWorld(左边) 另一个是 加法计算器(右边)的截图. 先运行第一个 程序看看效果 1.打开Xcode(没有哦mac系统的没有xcode的帮你们默哀 ...

  5. AJ学IOS(42)UI之核心动画CAAnimationGroup以及其他

    AJ分享,必须精品 效果: 代码: 很简单,不多说,就是把一堆动画放一起,看代码. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent * ...

  6. AJ学IOS(37)UI之CALayer

    AJ分享,必须精品 CALayer 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能 ...

  7. AJ学IOS(35)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。

    AJ分享,必须精品 效果: 实现步骤 其实这个实现起来不难 第一步先放好主要的UI,一张背景图和一个View 第二部就是把9个button放到view中,设置好按钮的默认和选中图片. 注意:创建时候的 ...

  8. AJ学IOS(27)UI之iOSUIKit字符属性NSAttributedString概述

    AJ分享,必须精品 UIKit字符属性NSAttributedString概述 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFon ...

  9. AJ学IOS(23)UI之控制器管理

    AJ分享,必须精品 控制器以及view的多种创建方式 控制器view的加载 通过storyboard创建 1:先加载storyboard⽂件(Test是storyboard的⽂文件名) UIStory ...

随机推荐

  1. 大数据安装之Kafka(用于实时处理的消息队列)

    一.安装部署kafka 1.集群规划 hadoop102                                 hadoop103                          hado ...

  2. Deep Protein Methylation Profiling by Combined Chemical and Immunoaffinity Approaches Reveals Novel PRMT1 Targets (结合层析法和免疫沉淀法的蛋白甲基化的深度检测技术发现了PRMT1新的靶标蛋白)

    题目:Deep Protein Methylation Profiling by Combined Chemical and Immunoaffinity Approaches Reveals Nov ...

  3. 【转】深入 ProtoBuf - 简介

    之前在网络通信和通用数据交换等应用场景中经常使用的技术是 JSON 或 XML,而在最近的开发中接触到了 Google 的 ProtoBuf. 在查阅相关资料学习 ProtoBuf 以及研读其源码之后 ...

  4. Delphi10.3的DBGrid中memo类型显示内容而不是(WIDEMEMO)

    1]连接好数据库,并显示: 2]增加所有字段: 3]添加事件: //    FDQuery1UserName: TWideMemoField; procedure TForm1.FDQuery1Use ...

  5. 学习mybatis框架时配置xml文件解决select莫名其妙报错问题

    遇到这种情况,如果语法没有错误,那就可能是你的eclipse在耍你!!! 怎么弄呢,重新建立一个文件,把原来的代码复制到新的文件中就ok啦!不用谢我,我叫雷锋

  6. C 2012年笔试题

    1指出程序段中的错误:分析错误的原因,并进行修改 1.1函数 swap 将两个字符串(字符数组作实参,长度不超过 100)的内容进行交换 void swap(char *pa,char *pb) { ...

  7. Python第四章-流程控制

    流程控制 在以前的代码中,所有的代码都是交由 Python 忠实地从头执行到结束.但是这些远远不够.很多时候需要根据不同的情况执行不同的代码. 如果你想改变这一工作流程,应该怎么做? 就像这样的情况: ...

  8. Python datetime 时间处理

    读入的时间数据是字符串格式,转换成datetime格式 data['time1'] = pd.to_datetime(data['time1'],format="%H:%M:%S:%f&qu ...

  9. mysql5.6配置文件详解(一)

    mysqld  Ver 5.6.11 for Linux on x86_64 (Source distribution)Copyright (c) 2000, 2013, Oracle and/or ...

  10. 干货 | Python进阶系列之学习笔记(四)

    目录 Python条件判断 Python循环语句 Python循环控制 迭代器与生成器 异常 一.Python 条件判断 如果某些条件满足,才能做某件事情:条件不满足时,则不能做,这就是所谓的判断. ...